cli icon indicating copy to clipboard operation
cli copied to clipboard

fix(libnpmpack): run scripts in foreground when foregroundScripts === true

Open winterqt opened this issue 3 years ago • 4 comments

Before this change, npm pack would not respect --foreground-scripts, so when {pre,post}pack scripts were run, the output would be polluted with output from those scripts.

I'm not sure how I'd test the output of this from libnpmpack tests, like the arborist test does. If anyone can give me any pointers on that, I'd be happy to add a test.

Also: tips on decreasing the length of the commit message are appreciated, the cutoff is a bit jarring.

References

Fixes #4121.

winterqt avatar Aug 26 '22 18:08 winterqt

This is failing due to the reduced test coverage. I can't figure out a way to test this behavior just from using libnpmpack -- I'd have to either spawn a new process or mock spawn.

What's the best way to go about this?

winterqt avatar Sep 02 '22 03:09 winterqt

This is failing due to the reduced test coverage. I can't figure out a way to test this behavior just from using libnpmpack -- I'd have to either spawn a new process or mock spawn.

What's the best way to go about this?

While we haven't started using it in this repo, mocking spawn is a thing we do in other npm cli subdependencies, and in the npm tests too. We use https://github.com/wraithgar/spawk. For good patterns you can check out the start stop and test tests in the npm cli itself.

tips on decreasing the length of the commit message are appreciated, the cutoff is a bit jarring.

fix: obey foregroundScript would probably suffice. The commit will only end up in the libnpmpack changelog so you don't really need to add that to the scope.

wraithgar avatar Sep 02 '22 23:09 wraithgar

@wraithgar I attempted to use spawk here, but am running into an issue. The command and arguments that are passed to spawn line up with what I'm mocking, but it still says that it wasn't called. Am I missing something obvious here?

winterqt avatar Sep 03 '22 01:09 winterqt

It looks like overriding child_process.spawn isn't working here, but I don't know why it would work in the other tests but not this one. (Maybe the package boundary?)

winterqt avatar Sep 04 '22 02:09 winterqt

Hey @wraithgar, just bumping this in case you forgot about my last comment :)

winterqt avatar Sep 26 '22 01:09 winterqt

I did see it. We are currently focused on breaking changes for npm 9 and there isn't any bandwidth right now for anyone to help you on this. If you can figure the tests out, great. If not, this can wait a bit till someone can look at it.

wraithgar avatar Sep 26 '22 14:09 wraithgar

I figured it out -- spawk needed to be initialized before everything else was imported. (Technically this would work all the same if I moved it before just a subset of the imports, but I figured this is best.)

winterqt avatar Oct 03 '22 01:10 winterqt

I guess I have to update package-lock.json. Do I just run npm ci to do that properly (so I don't update any other versions in the lockfile), or is there some other way to do it?

winterqt avatar Oct 03 '22 14:10 winterqt

How did you add spawk? If you used npm install it should have updated the lockfile accordingly.

wraithgar avatar Oct 03 '22 18:10 wraithgar

npm install -D spawk -ws libnpmpack

wraithgar avatar Oct 03 '22 18:10 wraithgar

npm install -D spawk -ws libnpmpack

After reverting workspaces/libnpmpack/package.json and DEPENDENCIES.md to their state before this commit, and running this command, I get these errors.

winterqt avatar Oct 04 '22 03:10 winterqt

Well fiddlesticks. Good news is you found a case that causes that error to happen so we can debug it now. Bad news is you can't install and update the md file. Go ahead and install with --ignore-scripts which will prevent that script from trying to update the md file. We may have to fix it to get CI to go green but at least you can get everything else about the PR ready to go.

Can you also make sure your branch is rebased against latest? It's possible that some things were moved around since you started. Lots of things have been shuffling as part of npm 9.

wraithgar avatar Oct 04 '22 13:10 wraithgar

@winterqt I am so sorry I told you to run the wrong command. It should be -w not -ws.

wraithgar avatar Oct 04 '22 16:10 wraithgar

I feel really bad about this. You've worked really hard on this PR and I know it's probably frustrating that this is the part we're getting blocked on. The PR itself looks great and once we sort the problem of adding spawk out this will land.

wraithgar avatar Oct 04 '22 16:10 wraithgar

In the interest of getting this out today (release day!) I went ahead and fixed the spawk install in another branch, leaving your work in its own commit: https://github.com/npm/cli/pull/5645

wraithgar avatar Oct 05 '22 17:10 wraithgar