Skip to content
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
38 changes: 38 additions & 0 deletions compiler/checker/go_packages_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,44 @@ func Greet(name string) string { return "hello " + name }
}
}

func TestCheckerReportsMissingGoModForProjectFFIShorthand(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "ard.toml"), []byte("name = \"app\"\nard = \">= 0.1.0\"\n"), 0o644); err != nil {
t.Fatal(err)
}
ffiDir := filepath.Join(root, "ffi")
if err := os.MkdirAll(ffiDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(ffiDir, "ffi.go"), []byte("package ffi\n\nfunc Greet() string { return \"hello\" }\n"), 0o644); err != nil {
t.Fatal(err)
}

mainPath := filepath.Join(root, "main.ard")
result := parse.Parse([]byte("use go:app/ffi\n"), mainPath)
if len(result.Errors) > 0 {
t.Fatalf("parse errors: %v", result.Errors)
}
moduleResolver, err := checker.NewModuleResolver(root)
if err != nil {
t.Fatal(err)
}
checked := checker.New(mainPath, result.Program, moduleResolver)
checked.Check()

for _, diagnostic := range checked.Diagnostics() {
if diagnostic.Code != checker.DiagnosticCodeGoImportResolution {
continue
}
want := "project-local Go FFI requires a go.mod; run `go mod init app` in " + root
if diagnostic.Text != want {
t.Fatalf("diagnostic text = %q, want %q", diagnostic.Text, want)
}
return
}
t.Fatalf("missing Go import resolution diagnostic: %#v", checked.Diagnostics())
}

func TestCheckerCanonicalizesProjectGoImportShorthands(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "ard.toml"), []byte("name = \"app\"\nard = \">= 0.1.0\"\n\n[go.imports]\nappgo = \"example.com/owner/app\"\n"), 0o644); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion compiler/checker/module_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,7 @@ func (mr *ModuleResolver) resolveGoImportPath(importerModulePath string, importP
if pkg.Name != "" && (importPath == ffiRoot || strings.HasPrefix(importPath, ffiRoot+"/")) {
modulePath, err := readGoModulePath(pkg.RootPath)
if os.IsNotExist(err) {
return importPath, nil
return "", fmt.Errorf("project-local Go FFI requires a go.mod; run `go mod init %s` in %s", pkg.Name, pkg.RootPath)
}
if err != nil {
return "", fmt.Errorf("read Go module for Ard package %q: %w", pkg.Name, err)
Expand Down
2 changes: 1 addition & 1 deletion website/src/content/docs/advanced/go-interop.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use go:image as image
use go:net/http as gohttp
```

A project that imports Go packages relies on the project's `go.mod`. Add Go dependencies with ordinary Go tooling, such as `go get`, before building the Ard project.
Go standard-library imports work without a `go.mod`. Third-party and project-local Go imports require a source `go.mod`; add them with ordinary Go tooling such as `go mod init` and `go get` before building the Ard project. The synthetic module created for generated Go output does not provide the source module context used during checking.

## Project FFI Bindings

Expand Down
Loading