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

Fix npm access error(#665) #666

Merged
merged 18 commits into from Dec 27, 2022
Merged

Fix npm access error(#665) #666

merged 18 commits into from Dec 27, 2022

Conversation

DerTimonius
Copy link
Contributor

@DerTimonius DerTimonius commented Nov 15, 2022

Thank you for considering this PR This is my first contribution to open source, so if this PR description is a bit too much, please let me know πŸ˜ƒ

This PR fixes the npm access error that occurs after updating npm to version 9.0.0.

βœ– Command failed with exit code 1: npm access ls-collaborators @upleveled/eslint-config-upleveled
npm ERR! code EUSAGE
npm ERR!
npm ERR! ls-collaborators is not a valid access command
npm ERR!
npm ERR! Set access level on published packages
npm ERR!
npm ERR! Usage:
npm ERR! npm access list packages [<user>|<scope>|<scope:team> [<package>]
npm ERR! npm access list collaborators [<package> [<user>]]
npm ERR! npm access get status [<package>]
npm ERR! npm access set status=public|private [<package>]
npm ERR! npm access set mfa=none|publish|automation [<package>]
npm ERR! npm access grant <read-only|read-write> <scope:team> [<package>]
npm ERR! npm access revoke <scope:team> [<package>]
npm ERR!
npm ERR! Options:
npm ERR! [--json] [--otp <otp>] [--registry <registry>]
npm ERR!
npm ERR! Run "npm help access" for more info

According to the npm documentation the npm access subcommands have been changed from ls-collaborators to list collaborators.

Switching

const args = ['access', 'ls-collaborators', packageName];

to

const args = ['access', 'list collaborators', packageName];

did not work, it keeps failing with the same errors.
Changing the execa arguments (on line 77) from npm to npm access list collaborators while stripping the args variable eliminated the error.


Fixes #665

@DerTimonius DerTimonius changed the title Fix npm access error([#655](https://github.com/sindresorhus/np/issues/665#issue-1448125592) Fix npm access error(#655) Nov 15, 2022
@DerTimonius DerTimonius changed the title Fix npm access error(#655) Fix npm access error(#665) Nov 15, 2022
@DerTimonius DerTimonius marked this pull request as ready for review November 15, 2022 21:43
source/npm/util.js Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

const args = ['access', 'list collaborators', packageName];

You have a typo. Should be:

const args = ['access', 'list', 'collaborators', packageName];

@sindresorhus
Copy link
Owner

Make sure that npm test passes locally and to review your own diff

@DerTimonius
Copy link
Contributor Author

Thank you for your feedback!
I had troubles running the tests on my (windows) machine yesterday, found a workaround but that clearly did not work.
I changed the line as you suggested, I also changed two tests of the prerequisites-tasks.js because it used the old npm access subcommands (not committed yet).
But running the tests (with the help of cross-env) now, I'm getting the following error with this index Β» version is invalid and index Β» version is pre-release:

TimeoutError {
     context: {},
     message: 'Connection to npm registry timed out',
 }

@DerTimonius
Copy link
Contributor Author

So, I now committed the changes to the test files as well and now the Github Actions tests run without error on my forked repo πŸ‘

@@ -68,7 +68,7 @@ exports.collaborators = async pkg => {
const packageName = pkg.name;
ow(packageName, ow.string);

const args = ['access', 'ls-collaborators', packageName];
const args = ['access', 'list', 'collaborators', packageName];
Copy link

Choose a reason for hiding this comment

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

this needs a --json as well

@schuchard
Copy link

Hey @DerTimonius, looks like you're pretty close. Anything I can do to help?

@DerTimonius
Copy link
Contributor Author

Hey @schuchard , thank you for your offer!
I did not have time in the last two days to take a look into this again, and now I added the --json flag. I'm not sure if I missed anything else though, so if I did, any help would be appreciated πŸ˜ƒ

@schuchard
Copy link

Hi @sindresorhus, is there anything else needed to merge this PR? Thanks.

@DerTimonius
Copy link
Contributor Author

I don't know why the tests are failing now with an error message that should be solved (npm access list not a valid argument).

@karlhorky
Copy link
Contributor

karlhorky commented Nov 22, 2022

Maybe it's failing on an older version of npm? (eg. one that does not yet have npm access list ... commands)

Maybe it should be conditional on what version of npm is being run.

@schuchard
Copy link

Seems likely. NPM 9 brought a breaking change to npm access:

npm access subcommands have been renamed

If this package intends to support npm < 9, conditional logic and tests are needed.

@DerTimonius
Copy link
Contributor Author

I'm making progress: I implemented a npm version check, so that the correct subcommands are used.

Currently I have one failing test left:

prerequisite-tasks β€Ί should fail when user is not authenticated at external registry

142:   await t.throwsAsync(run(testedModule('2.0.0', {name: 'test', version:…
   143:     {message: 'You do not have write permissions required to publish th…

  Promise resolved with:

  undefined

Locally, I always get a Timeout Error on this test though...

@DerTimonius
Copy link
Contributor Author

Any hints on what I'm missing?

@karlhorky
Copy link
Contributor

karlhorky commented Dec 5, 2022

So this is the error message that you're getting:

  1 test failed

  prerequisite-tasks β€Ί should fail when user is not authenticated at external registry

  /home/runner/work/np/np/test/prerequisite-tasks.js:145

   144:   process.env.NODE_ENV = 'P';                                           
   145:   await t.throwsAsync(run(testedModule('2.0.0', {name: 'test', version:…
   146:     {message: 'You do not have write permissions required to publish th…

  Promise rejected with unexpected exception:

  Error {
    context: {},
    message: 'Git tag `v2.0.0` already exists.',
  }

  Expected message to equal:

  'You do not have write permissions required to publish this package.'

  Object.exports.verifyTagDoesNotExistOnRemote (source/git-util.js:206:9)
  Task.task (source/prerequisite-tasks.js:88:5)

This indicates that the collaborators JSON here actually passed the test:

const json = JSON.parse(collaborators);
const permissions = json[username];
if (!permissions || !permissions.includes('write')) {
throw new Error('You do not have write permissions required to publish this package.');
}

You could try logging out this value to see if this is truly the case.

Or, second idea: maybe the check for list collaborators is not being run at all... πŸ€” Seems like you have a weird pattern here:

const testArgs = async () => {
const npmVersion = await versionNpm();
return semver.satisfies(npmVersion, '>=9.0.0') ? ['list collaborators', ' --json'] : ['ls-collaborators', ''];
};

To me testArgs should be an array, not a function...

@karlhorky
Copy link
Contributor

karlhorky commented Dec 5, 2022

Last tip, the order of your arguments in these three places is different:

const args = semver.satisfies(npmVersion, '>=9.0.0') ? ['access', 'list', 'collaborators', '--json', packageName] : ['access', 'ls-collaborators', packageName];

command: `npm access ${testArgs[0]} test${testArgs[1]}`,

command: `npm access ${testArgs[0]} test${testArgs[1]} --registry http://my.io`,

@DerTimonius
Copy link
Contributor Author

Thank you for your inputs @karlhorky !

After logging out a few things, I narrowed the problem down to an issue with the external registry:

  • The collaborators functions return false and the task does not get to the permissions variable.
  • Logging the args inside the collaborators function returns a valid array.
  • But since it returned false, the only place where this could happen is here in source/npm/util.js, lines 78-88:
try {
		const {stdout} = await execa('npm', args);
		return stdout;
	} catch (error) {
		// Ignore non-existing package error
		if (error.stderr.includes('code E404')) {
			return false;
		}

		throw error;
	}

So, logging out the error here gives me this message:

'npm ERR! code E404\n' +
    'npm ERR! 404 Not Found - GET http://my.io/-/package/test/collaborators\n' +
    '\n' +
    'npm ERR! A complete log of this run can be found in:\n'

But the command is this:

npm access list collaborators --json test --registry http://my.io

I don't know where the -/package/test/collaborators is coming from, it's not added in the code... (or if this is even the issue).

Regarding this:

Or, second idea: maybe the check for list collaborators is not being run at all... πŸ€” Seems like you have a weird pattern here:

const testArgs = async () => {
const npmVersion = await versionNpm();
return semver.satisfies(npmVersion, '>=9.0.0') ? ['list collaborators', ' --json'] : ['ls-collaborators', ''];
};

To me testArgs should be an array, not a function...

You're right, I tried to do is as an array in the first place, but wasn't able to for some reason. Put it inside the test now, just to be sure.

@DerTimonius
Copy link
Contributor Author

So, I removed the conditional arguments in the tests since it does not have any effect on the executed code, now it's again this

command: 'npm access ls-collaborators test',

instead of:

command: `npm access ${testArgs[0]} test${testArgs[1]}',

I suspected that maybe this change could be the reason the tests are failing.
Also I moved the --json flag once again.

@DerTimonius
Copy link
Contributor Author

Hey @sindresorhus, is there anything else I have to change?

@sindresorhus sindresorhus merged commit 0f5dd33 into sindresorhus:main Dec 27, 2022
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.

npm 9.0.0 Support?
5 participants