Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disallow modules to use reserved names #4243

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions cmd/ftl/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package main
import (
"context"
"fmt"
"go/token"
"os"
"path/filepath"
"regexp"
"time"

"github.com/alecthomas/kong"
Expand All @@ -32,11 +30,6 @@ func (i newCmd) Run(ctx context.Context, ktctx *kong.Context, config projectconf
return err
}

// Validate the module name with custom validation
if !isValidModuleName(name) {
return fmt.Errorf("module name %q must be a valid Go module name and not a reserved keyword", name)
}

logger := log.FromContext(ctx)
logger.Debugf("Creating FTL %s module %q in %s", i.Language, name, path)

Expand Down Expand Up @@ -64,6 +57,7 @@ func (i newCmd) Run(ctx context.Context, ktctx *kong.Context, config projectconf
if err != nil {
return fmt.Errorf("could not acquire file lock: %w", err)
}
logger.Infof("acquired file lock for %s", config.WatchModulesLockPath())
defer release() //nolint:errcheck

err = plugin.CreateModule(ctx, config, moduleConfig, flags)
Expand Down Expand Up @@ -96,7 +90,7 @@ func validateModule(dir string, name string) (string, string, error) {
if name == "" {
name = filepath.Base(dir)
}
if !schema.ValidateName(name) {
if !schema.ValidateModuleName(name) {
return "", "", fmt.Errorf("module name %q is invalid", name)
}
path := filepath.Join(dir, name)
Expand All @@ -109,14 +103,3 @@ func validateModule(dir string, name string) (string, string, error) {
}
return name, absPath, nil
}

func isValidModuleName(name string) bool {
validNamePattern := regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_]*$`)
if !validNamePattern.MatchString(name) {
return false
}
if token.Lookup(name).IsKeyword() {
return false
}
return true
}
11 changes: 10 additions & 1 deletion common/schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package schema

import (
"fmt"
"go/token"
"net/http"
"reflect"
"regexp"
Expand Down Expand Up @@ -245,6 +246,14 @@ func ValidateName(name string) bool {
return validNameRe.MatchString(name)
}

// ValidateModuleName validates an FTL module name.
func ValidateModuleName(name string) bool {
if token.Lookup(name).IsKeyword() {
return false
}
return ValidateName(name)
}

// Validate performs the subset of semantic validation possible on a single module.
//
// It ignores references to other modules.
Expand All @@ -253,7 +262,7 @@ func (m *Module) Validate() error {

scopes := NewScopes()

if !ValidateName(m.Name) {
if !ValidateModuleName(m.Name) {
merr = append(merr, errorf(m, "module name %q is invalid", m.Name))
}
if m.Builtin && m.Name != "builtin" {
Expand Down
10 changes: 10 additions & 0 deletions common/schema/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,16 @@ func TestValidate(t *testing.T) {
`9:6: verb can not subscribe to multiple topics`,
},
},
{
name: "ModuleNameCantBeGoKeyword",
schema: `
module map {
}
`,
errs: []string{
`2:4: module name "map" is invalid`,
},
},
}

for _, test := range tests {
Expand Down
Loading