Skip to content

Commit

Permalink
Merge pull request #570 from nevalang/fix_cli
Browse files Browse the repository at this point in the history
fix(cli): use module root to correctly identify main pkg name (path)
  • Loading branch information
emil14 committed Apr 26, 2024
2 parents fcc17dc + 771f9a4 commit dfea7f2
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/neva",
"cwd": "${workspaceFolder}",
"args": ["run", "examples/struct_selector"]
"cwd": "${workspaceFolder}/e2e/run_cli_not_from_module_root/foo",
"args": ["run", "bar"]
},
// === LSP ===
{
Expand Down
2 changes: 1 addition & 1 deletion cmd/lsp/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Indexer struct {
}

func (i Indexer) FullIndex(ctx context.Context, path string) (src.Build, *compiler.Error, error) {
rawBuild, err := i.builder.Build(ctx, path)
rawBuild, _, err := i.builder.Build(ctx, path)
if err != nil {
return src.Build{}, nil, fmt.Errorf("builder: %w", err)
}
Expand Down
55 changes: 55 additions & 0 deletions e2e/run_cli_not_from_module_root/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package test

// in this file we test files designed specifically for e2e.

import (
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

// Test that CLI will go from up to down and find module's manifest
func Test_UpperThanManifest(t *testing.T) {
// go one level up (and go back after test is finished)
wd, err := os.Getwd()
require.NoError(t, err)
defer os.Chdir(wd)
require.NoError(t, os.Chdir(".."))

cmd := exec.Command("neva", "run", "run_cli_not_from_module_root/foo/bar")

out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(
t,
"42\n",
string(out),
)

require.Equal(t, 0, cmd.ProcessState.ExitCode())
}

// Test that CLI will go from down to up and find module's manifest
func Test_DownToManifest(t *testing.T) {
t.Skip() // FIXME https://github.com/nevalang/neva/issues/571

// go one level down (and go back after test is finished)
wd, err := os.Getwd()
require.NoError(t, err)
defer os.Chdir(wd)
require.NoError(t, os.Chdir("foo"))

cmd := exec.Command("neva", "run", "bar")

out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(
t,
"42\n",
string(out),
)

require.Equal(t, 0, cmd.ProcessState.ExitCode())
}
4 changes: 4 additions & 0 deletions e2e/run_cli_not_from_module_root/foo/bar/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
component Main(start) (stop) {
nodes { Println }
net { :start -> (42 -> println -> :stop) }
}
1 change: 1 addition & 0 deletions e2e/run_cli_not_from_module_root/neva.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
neva: 0.10.0
20 changes: 10 additions & 10 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type ManifestParser interface {
func (b Builder) Build(
ctx context.Context,
wd string,
) (compiler.RawBuild, *compiler.Error) {
) (compiler.RawBuild, string, *compiler.Error) {
// load entry module from disk
entryMod, err := b.LoadModuleByPath(ctx, wd)
entryMod, entryModRootPath, err := b.LoadModuleByPath(ctx, wd)
if err != nil {
return compiler.RawBuild{}, &compiler.Error{
return compiler.RawBuild{}, "", &compiler.Error{
Err: fmt.Errorf("build entry mod: %w", err),
}
}
Expand All @@ -48,9 +48,9 @@ func (b Builder) Build(
}

// load stdlib module
stdMod, err := b.LoadModuleByPath(ctx, b.stdLibPath)
stdMod, _, err := b.LoadModuleByPath(ctx, b.stdLibPath)
if err != nil {
return compiler.RawBuild{}, &compiler.Error{
return compiler.RawBuild{}, "", &compiler.Error{
Err: fmt.Errorf("build stdlib mod: %w", err),
}
}
Expand All @@ -60,7 +60,7 @@ func (b Builder) Build(

release, err := acquireLockFile()
if err != nil {
return compiler.RawBuild{}, &compiler.Error{
return compiler.RawBuild{}, "", &compiler.Error{
Err: fmt.Errorf("failed to acquire lock file: %w", err),
}
}
Expand All @@ -77,14 +77,14 @@ func (b Builder) Build(

depWD, _, err := b.downloadDep(depModRef)
if err != nil {
return compiler.RawBuild{}, &compiler.Error{
return compiler.RawBuild{}, "", &compiler.Error{
Err: fmt.Errorf("download dep: %w", err),
}
}

depMod, err := b.LoadModuleByPath(ctx, depWD)
depMod, _, err := b.LoadModuleByPath(ctx, depWD)
if err != nil {
return compiler.RawBuild{}, &compiler.Error{
return compiler.RawBuild{}, "", &compiler.Error{
Err: fmt.Errorf("build dep mod: %w", err),
}
}
Expand All @@ -103,7 +103,7 @@ func (b Builder) Build(
return compiler.RawBuild{
EntryModRef: src.ModuleRef{Path: "@"},
Modules: mods,
}, nil
}, entryModRootPath, nil
}

func getThirdPartyPath() (string, error) {
Expand Down
8 changes: 4 additions & 4 deletions internal/builder/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ import (
func (p Builder) LoadModuleByPath(
ctx context.Context,
wd string,
) (compiler.RawModule, error) {
) (compiler.RawModule, string, error) {
manifest, modRootPath, err := p.getNearestManifest(wd)
if err != nil {
return compiler.RawModule{}, fmt.Errorf("retrieve manifest: %w", err)
return compiler.RawModule{}, "", fmt.Errorf("retrieve manifest: %w", err)
}

pkgs := map[string]compiler.RawPackage{}
if err := retrieveSourceCode(modRootPath, pkgs); err != nil {
return compiler.RawModule{}, fmt.Errorf("walk: %w", err)
return compiler.RawModule{}, "", fmt.Errorf("walk: %w", err)
}

return compiler.RawModule{
Manifest: manifest,
Packages: pkgs,
}, nil
}, modRootPath, nil
}

// retrieveSourceCode recursively walks the given tree and fills given pkgs with neva files
Expand Down
4 changes: 2 additions & 2 deletions internal/builder/tests/intergration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestBuilder_WDIsModRoot(t *testing.T) {
prsr := parser.New(false)
bldr := builder.MustNew(prsr)

build, err := bldr.Build(context.Background(), "testmod")
build, _, err := bldr.Build(context.Background(), "testmod")
require.True(t, err == nil)

mod, ok := build.Modules[build.EntryModRef]
Expand All @@ -36,7 +36,7 @@ func TestBuilder_WDIsPkg(t *testing.T) {
prsr := parser.New(false)
bldr := builder.MustNew(prsr)

build, err := bldr.Build(context.Background(), "testmod/do_nothing")
build, _, err := bldr.Build(context.Background(), "testmod/do_nothing")
require.True(t, err == nil)

mod, ok := build.Modules[build.EntryModRef]
Expand Down
21 changes: 14 additions & 7 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ func (c Compiler) Compile(
return c.backend.Emit(dstPath, ir)
}

func (c Compiler) CompileToIR(whereCLIExecuted string, whereEntryPkg string) (*ir.Program, *Error) {
rawBuild, err := c.builder.Build(
func (c Compiler) CompileToIR(
wd string,
mainPkgPath string,
) (*ir.Program, *Error) {
rawBuild, entryModRoot, err := c.builder.Build(
context.Background(),
whereEntryPkg,
mainPkgPath,
)
if err != nil {
return nil, Error{
Location: &sourcecode.Location{
PkgName: whereEntryPkg,
PkgName: mainPkgPath,
},
}.Wrap(err)
}
Expand All @@ -54,9 +57,13 @@ func (c Compiler) CompileToIR(whereCLIExecuted string, whereEntryPkg string) (*i
Modules: parsedMods,
}

whereEntryPkg = strings.TrimPrefix(whereEntryPkg, "./")
mainPkgPath = strings.TrimPrefix(mainPkgPath, "./")
mainPkgPath = strings.TrimPrefix(mainPkgPath, entryModRoot+"/")

analyzedBuild, err := c.analyzer.AnalyzeExecutableBuild(parsedBuild, whereEntryPkg)
analyzedBuild, err := c.analyzer.AnalyzeExecutableBuild(
parsedBuild,
mainPkgPath,
)
if err != nil {
return nil, err
}
Expand All @@ -66,7 +73,7 @@ func (c Compiler) CompileToIR(whereCLIExecuted string, whereEntryPkg string) (*i
return nil, err
}

irProg, err := c.irgen.Generate(desugaredBuild, whereEntryPkg)
irProg, err := c.irgen.Generate(desugaredBuild, mainPkgPath)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (

type (
Builder interface {
Build(ctx context.Context, wd string) (RawBuild, *Error)
Build(ctx context.Context, wd string) (RawBuild, string, *Error)
}

RawBuild struct {
Expand Down

0 comments on commit dfea7f2

Please sign in to comment.