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

feat(task): Add support for passing down cwd to spawn steps #3209

Closed
wants to merge 3 commits into from

Conversation

icj217
Copy link
Contributor

@icj217 icj217 commented Dec 28, 2023

The existing documentation around the cwd task/step property doesn't explicitly state that it only works for exec steps, but that's currently the case. There are use cases where having spawn tasks inherit the cwd of the task that spawned them is quite useful. For example:

const sdkBuildTask = project.addTask('sdk:build', { cwd: 'my_folder' });
sdkBuildTask.spawn(project.poetry.poetryTask, { args: ['version', '$VERSION' });
sdkBuildTask.spawn(project.poetry.poetryTask, { args: ['build'] });

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@codecov-commenter
Copy link

codecov-commenter commented Dec 28, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (9482497) 96.32% compared to head (e992213) 96.32%.

Files Patch % Lines
src/task-runtime.ts 87.50% 2 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3209   +/-   ##
=======================================
  Coverage   96.32%   96.32%           
=======================================
  Files         191      191           
  Lines       36488    36498   +10     
  Branches     3399     3399           
=======================================
+ Hits        35147    35158   +11     
+ Misses       1341     1340    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 344 to 361
test("cwd can be passed down to spawn steps", () => {
const p = new TestProject();
const taskcwd = join(p.outdir, "mypwd");
const stepcwd = join(p.outdir, "yourpwd");
mkdirSync(taskcwd, { recursive: true });
mkdirSync(stepcwd, { recursive: true });
const echo = p.addTask("echo", { exec: "echo", receiveArgs: true });
const task = p.addTask("testme", { cwd: taskcwd });
task.spawn(echo, { args: ["step1=$PWD"] });
task.spawn(echo, { args: ["step1=$PWD"], cwd: stepcwd });

const noCwdSpecified = executeTask(p, "echo", {}, ["$PWD"]);
expect(noCwdSpecified[0]).toContain(p.outdir);

const lines = executeTask(p, "testme");
expect(lines[0].includes("mypwd")).toBeTruthy();
expect(lines[1].includes("yourpwd")).toBeTruthy();
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I've tried hard to understand what this test is doing. Could you please refactor this to be more readable. I'd recommend sticking to the same names for things e.g. not taskcwd and mypwd and make the symbol names more explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I've updated the test to hopefully be clearer now.

Comment on lines 355 to 356
const noCwdSpecified = executeTask(p, "echo", {}, ["$PWD"]);
expect(noCwdSpecified[0]).toContain(p.outdir);
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems redundant since very task on this project will use the project's outdir.

@@ -153,7 +162,8 @@ class RunTask {
this.runtime.runTask(
step.spawn,
[...this.parents, this.task.name],
argsList
argsList,
{ cwd: step.cwd ?? task.cwd }
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like a breaking change to me. We are now giving the step.cwd preference over the task.cwd and there is no way to revert the original behavior?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah my struggle with implementing this is that it doesn't seem like the step-level cwd was originally intended to be used outside of exec steps, but none of the documentation indicates this, so it seems logical that if you set an explicit step-level cwd, then it should take precedence regardless of whether that step is an exec or a spawn. step.cwd as well as task.cwd can both be undefined, which would result in the default runtime working dir taking precedence (as seen on line 101)

We could add a new option to TaskSpec like spawnInheritCwd?: boolean to make this behavior explicit, but aesthetically that doesn't feel great.

Copy link
Contributor

@mrgrain mrgrain left a comment

Choose a reason for hiding this comment

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

Hi, this seems reasonable but I am not sure I follow the proposed change and the implications it has for existing code.

@mrgrain
Copy link
Contributor

mrgrain commented Mar 25, 2024

There will be bigger changes and some deprecations as part of #3162
Closing this one here in favor of those. Lesson learned is certainly we need to be more explicit about how cwd is inherited.

@mrgrain mrgrain closed this Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants