这是indexloc提供的服务,不要输入任何密码
Skip to content

Change fs.ReadPackageJSON to operate on AbsolutePath instances #1773

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

Merged
merged 2 commits into from
Aug 26, 2022

Conversation

gsoltis
Copy link
Contributor

@gsoltis gsoltis commented Aug 25, 2022

Updates fs.ReadPackageJSON to take an AbsolutePath so that we respect --cwd
Pulls the thread on updating PackageJSON.Dir and PackageJSON.PackageJSONPath to have the correct turbopath types.

cc @nathanhammond note in particular changes to taskhash_test.go

@vercel
Copy link

vercel bot commented Aug 25, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
turbo-site ⬜️ Ignored (Inspect) Visit Preview Aug 26, 2022 at 4:53PM (UTC)

Copy link
Contributor

@nathanhammond nathanhammond left a comment

Choose a reason for hiding this comment

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

Three changes requested, none of which are particularly material. I will rebase #1779 on top of this.

@@ -305,21 +304,25 @@ func (c *Context) populateTopologicGraphForPackageJSON(pkg *fs.PackageJSON, root
return nil
}

func (c *Context) parsePackageJSON(buildFilePath string) error {
func (c *Context) parsePackageJSON(repoRoot fs.AbsolutePath, pkgJSONPath fs.AbsolutePath) error {
c.mutex.Lock()
defer c.mutex.Unlock()

// log.Printf("[TRACE] reading package.json : %+v", buildFilePath)
Copy link
Contributor

Choose a reason for hiding this comment

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

(request for change)

Let's drop this log which now logs something that doesn't exist.

c.mutex.Lock()
defer c.mutex.Unlock()

// log.Printf("[TRACE] reading package.json : %+v", buildFilePath)
if fs.FileExists(buildFilePath) {
Copy link
Contributor

Choose a reason for hiding this comment

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

That implicit dependency on cwd. 😅

Comment on lines +324 to +325
pkg.PackageJSONPath = turbopath.AnchoredSystemPathFromUpstream(relativePkgJSONPath)
pkg.Dir = turbopath.AnchoredSystemPathFromUpstream(filepath.Dir(relativePkgJSONPath))
Copy link
Contributor

Choose a reason for hiding this comment

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

These are indeed both AnchoredSystemPaths. 👍

@@ -143,6 +144,7 @@ func WithGraph(repoRoot fs.AbsolutePath, rootPackageJSON *fs.PackageJSON, cacheD
}

// Get the workspaces from the package manager.
// workspaces are absolute paths
Copy link
Contributor

Choose a reason for hiding this comment

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

That moment when you finally find where you're going to stop pulling the thread.

@@ -154,12 +156,9 @@ func WithGraph(repoRoot fs.AbsolutePath, rootPackageJSON *fs.PackageJSON, cacheD
// until all parsing is complete
parseJSONWaitGroup := &errgroup.Group{}
for _, workspace := range workspaces {
relativePkgPath, err := filepath.Rel(rootpath, workspace)
if err != nil {
return fmt.Errorf("non-nested package.json path %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Note for readers: this error got moved into the function. It's currently impossible to reach this error case because of how the paths are generated. 🤷‍♂️

@@ -22,7 +22,7 @@ func Test_ReadTurboConfig(t *testing.T) {
rootDir := "testdata"
turboJSONPath := cwd.Join(rootDir)
packageJSONPath := cwd.Join(rootDir, "package.json")
rootPackageJSON, pkgJSONReadErr := ReadPackageJSON(packageJSONPath.ToStringDuringMigration())
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

@@ -824,7 +824,7 @@ func (r *run) executeDryRun(ctx gocontext.Context, engine *core.Scheduler, g *co
Package: pt.PackageName,
Hash: hash,
Command: command,
Dir: pt.Pkg.Dir,
Dir: pt.Pkg.Dir.ToString(),
Copy link
Contributor

Choose a reason for hiding this comment

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

(request for change)

Suggested change
Dir: pt.Pkg.Dir.ToString(),
Dir: pt.Pkg.Dir.ToStringDuringMigration(),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a JSON representation, not something we use for further computation. I think .ToString() is correct.

@@ -937,7 +937,8 @@ func (e *execContext) exec(ctx gocontext.Context, pt *nodes.PackageTask, deps da
}

cmd := exec.Command(e.packageManager.Command, argsactual...)
cmd.Dir = pt.Pkg.Dir
// TODO(gsoltis): I think this might be an actual bug? cmd.Dir should probably be an absolute path
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that this is a "bug" but I don't think it matters so long as you have to run at root.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe it is a real bug affecting --cwd. I have a follow-up PR to fix it.

@@ -24,7 +24,7 @@ func Test_manuallyHashPackage(t *testing.T) {
t.Fatalf("failed to create temp dir: %v", err)
}
repoRoot := turbopath.AbsoluteSystemPathFromUpstream(root)
pkgName := turbopath.RelativeSystemPath("libA")
pkgName := turbopath.AnchoredSystemPath("libA")
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a correct change, which hallelujah can be detected just from types.

@@ -51,7 +51,7 @@ func Test_manuallyHashPackage(t *testing.T) {
t.Fatalf("failed to write contents to .gitignore: %v", err)
}
rootIgnoreFile.Close()
pkgIgnoreFilename := repoRoot.Join(pkgName, ".gitignore")
pkgIgnoreFilename := repoRoot.Join(turbopath.RelativeSystemPath(pkgName.ToString()), ".gitignore")
Copy link
Contributor

Choose a reason for hiding this comment

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

(request for change)

This isn't supposed to work, I'm not actually sure why it does? Is that literal .gitignore somehow getting past the type check?

func (p AbsoluteSystemPath) Join(additional ...RelativeSystemPath) AbsoluteSystemPath {
	cast := RelativeSystemPathArray(additional)
	return AbsoluteSystemPath(filepath.Join(p.ToString(), filepath.Join(cast.ToStringArray()...)))
}

pkgIgnoreFilename := repoRoot.Join( // AbsoluteSystemPath
	turbopath.RelativeSystemPath(
		pkgName.ToString() // AnchoredSystemPath
	),
	".gitignore" // string, wat?
)

The way I envisioned doing this:

pkgName.RestoreAnchor(repoRoot).Join(turbopath.RelativeSystemPath(".gitignore");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

literals are a hole in Go's newtype implementation. They are implicitly treated as the expected type.

@gsoltis gsoltis added the pr: automerge Kodiak will merge these automatically after checks pass label Aug 26, 2022
@kodiakhq kodiakhq bot merged commit 0d51a1b into main Aug 26, 2022
@kodiakhq kodiakhq bot deleted the gsoltis/abs_path_read_json branch August 26, 2022 17:13
kodiakhq bot pushed a commit that referenced this pull request Aug 30, 2022
Setting `cmd.Dir` to an absolute path ensures that it will continue to work in the presence of the `--cwd` flag.

Bonus drop unused `turboCache` from `execContext`.

Note that this PR stacks on top of #1773
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: automerge Kodiak will merge these automatically after checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants