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: module dir paths and lsp error unwrapping #1705

Merged
merged 1 commit into from
Jun 7, 2024
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
2 changes: 1 addition & 1 deletion cmd/ftl/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type buildCmd struct {
func (b *buildCmd) Run(ctx context.Context, projConfig projectconfig.Config) error {
client := rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)
if len(b.Dirs) == 0 && len(b.External) == 0 {
b.Dirs = projConfig.ModuleDirsOrDefault()
b.Dirs = projConfig.AbsModuleDirsOrDefault()
b.External = projConfig.ExternalDirs
}
if len(b.Dirs) == 0 && len(b.External) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ftl/cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type devCmd struct {

func (d *devCmd) Run(ctx context.Context, projConfig projectconfig.Config) error {
if len(d.Dirs) == 0 && len(d.External) == 0 {
d.Dirs = projConfig.ModuleDirsOrDefault()
d.Dirs = projConfig.AbsModuleDirsOrDefault()
d.External = projConfig.ExternalDirs
}
if len(d.Dirs) == 0 && len(d.External) == 0 {
Expand Down
21 changes: 21 additions & 0 deletions common/projectconfig/merge.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package projectconfig

import (
"path/filepath"
)

// Merge configuration files.
//
// Config is merged left to right, with later files taking precedence over earlier files.
Expand All @@ -11,7 +15,24 @@ func Merge(paths ...string) (Config, error) {
return config, err
}
config = merge(config, partial)

// Make module-dirs absolute to mimic the behavior of the CLI
config.absModuleDirs = []string{}
for _, dir := range config.ModuleDirs {
if !filepath.IsAbs(dir) {
absDir := filepath.Join(filepath.Dir(path), dir)
config.absModuleDirs = append(config.absModuleDirs, absDir)
} else {
config.absModuleDirs = append(config.absModuleDirs, dir)
}
}

// If no module-dirs are defined, default to the directory of the config file
if len(config.absModuleDirs) == 0 {
config.absModuleDirs = []string{filepath.Dir(path)}
}
}

return config, nil
}

Expand Down
11 changes: 4 additions & 7 deletions common/projectconfig/projectconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,12 @@ type Config struct {
ExternalDirs []string `toml:"external-dirs"`
Commands Commands `toml:"commands"`
FTLMinVersion string `toml:"ftl-min-version"`
absModuleDirs []string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit gross, but I don't have any better ideas.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had exactly the same feeling. I'm also not 100% sure how we want the abs paths to be computed given that it's possible reference multiple ftl-project.toml files, but those files could be at different paths. Which, is what led me to this approach so that at least the modules will be relative to the ftl-project.toml file.

Definitely feels like this will need more thought in the future though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and also the working directory of the process could change after construction, so we can't rely on dynamically making them absolute. It seems like the best solution.

}

// ModuleDirsOrDefault returns the module-dirs field from the ftl-project.toml, unless
// AbsModuleDirsOrDefault returns the absolute path for the module-dirs field from the ftl-project.toml, unless
// that is not defined, in which case it defaults to the root directory.
func (c Config) ModuleDirsOrDefault() []string {
if len(c.ModuleDirs) > 0 {
return c.ModuleDirs
}
return []string{"."}
}
func (c Config) AbsModuleDirsOrDefault() []string { return c.absModuleDirs }

// ConfigPaths returns the computed list of configuration paths to load.
func ConfigPaths(input []string) []string {
Expand Down Expand Up @@ -136,6 +132,7 @@ func loadFile(path string) (Config, error) {
}
return Config{}, fmt.Errorf("unknown configuration keys: %s", strings.Join(keys, ", "))
}

return config, nil
}

Expand Down
7 changes: 5 additions & 2 deletions lsp/lsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ func (s *Server) post(err error) {
errUnspecified := []error{}

// Deduplicate and associate by filename.
for _, err := range ftlErrors.DeduplicateErrors(ftlErrors.UnwrapAll(err)) {
for _, e := range ftlErrors.DeduplicateErrors(ftlErrors.UnwrapAll(err)) {
if !ftlErrors.Innermost(e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

niiiiice :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All @worstell here 😂

continue
}
var ce *schema.Error
if errors.As(err, &ce) {
if errors.As(e, &ce) {
filename := ce.Pos.Filename
if _, exists := errByFilename[filename]; !exists {
errByFilename[filename] = errSet{}
Expand Down
Loading