Skip to content

Conversation

@ovcharenko
Copy link

That will fix pollution of installation path with TypeScript output:

E           pre_commit.util.CalledProcessError: command: ('/private/var/folders/sd/qk6llt717b99lp1wgtym5qwm0000gn/T/pytest-of-local.user/pytest-16/test_node_typescript_hook_syst0/node_env-system/bin/node', '/opt/homebrew/bin/npm', 'install', '-g', '/private/var/folders/sd/qk6llt717b99lp1wgtym5qwm0000gn/T/pytest-of-local.user/pytest-16/test_node_typescript_hook_syst0/> t@0.0.1 prepare\n> tsc\n\nt-0.0.1.tgz')
E           return code: 254
E           stdout: (none)
E           stderr:
E               npm warn tarball tarball data for file:/private/var/folders/sd/qk6llt717b99lp1wgtym5qwm0000gn/T/pytest-of-local.user/pytest-16/test_node_typescript_hook_syst0/> t@0.0.1 prepare> tsct-0.0.1.tgz (null) seems to be corrupted. Trying again.
E               npm warn tarball tarball data for file:/private/var/folders/sd/qk6llt717b99lp1wgtym5qwm0000gn/T/pytest-of-local.user/pytest-16/test_node_typescript_hook_syst0/> t@0.0.1 prepare> tsct-0.0.1.tgz (null) seems to be corrupted. Trying again.
E               npm error code ENOENT
E               npm error syscall open
E               npm error path /private/var/folders/sd/qk6llt717b99lp1wgtym5qwm0000gn/T/pytest-of-local.user/pytest-16/test_node_typescript_hook_syst0/> t@0.0.1 prepare> tsct-0.0.1.tgz
E               npm error errno -2
E               npm error enoent ENOENT: no such file or directory, open '/private/var/folders/sd/qk6llt717b99lp1wgtym5qwm0000gn/T/pytest-of-local.user/pytest-16/test_node_typescript_hook_syst0/> t@0.0.1 prepare> tsct-0.0.1.tgz'
E               npm error enoent This is related to npm not being able to find a file.
E               npm error enoent
E               npm error A complete log of this run can be found in: /Users/local.user/.npm/_logs/2025-05-09T10_21_58_861Z-debug-0.log

@asottile
Copy link
Member

asottile commented May 9, 2025

the other branch which tried to solve this is here and my recommendation: #3259 (comment)

@ovcharenko
Copy link
Author

the other branch which tried to solve this is here and my recommendation: #3259 (comment)

That doesn't help much IMHO, because the JSON is part of other noisy output. So instead of one-line fix you will have to find JSON object and handle all error parsing.

@asottile
Copy link
Member

asottile commented May 9, 2025

pretty sure in json mode it does a better job of obscuring the nonsense

@ovcharenko
Copy link
Author

pretty sure in json mode it does a better job of obscuring the nonsense

19d0b42 should do this, right?

@ovcharenko ovcharenko changed the title fix: Process only the last line of npm pack output. fix: Proper way of finding npm pack filename output. May 9, 2025
@ovcharenko
Copy link
Author

@asottile any chance to get this merged any soon?

Comment on lines 141 to 143
"devDependencies": {
"typescript": "^5.3.0"
}
Copy link
Member

Choose a reason for hiding this comment

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

this is going to make the test prohibitively slow -- can you make a faster example?

Copy link
Author

Choose a reason for hiding this comment

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

@asottile I took an example from #3259 instead. Will that work?

Comment on lines +241 to +250


def get_npm_version() -> tuple[int, ...]:
_, out, _ = cmd_output('npm', '--version')
version_match = re.match(r'^(\d+)\.(\d+)\.(\d+)', out)

if version_match is None:
return 0, 0, 0
else:
return tuple(map(int, version_match.groups()))
Copy link
Member

Choose a reason for hiding this comment

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

new code should not end up in utils -- also this is only used in one place

Comment on lines +106 to +108
# https://docs.npmjs.com/cli/v11/using-npm/changelog#1100-pre0-2024-11-26
if npm_version >= (11, 0, 0):
args.append('--ignore-scripts')
Copy link
Member

Choose a reason for hiding this comment

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

this seems unrelated to your patch / not necessary

Comment on lines +113 to +114
except json.JSONDecodeError as e:
raise ValueError('Failed to parse npm pack output as JSON.') from e
Copy link
Member

Choose a reason for hiding this comment

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

just let it raise no reason to catch and reraise

Comment on lines +116 to +127
if not pkg_json:
raise ValueError('JSON array from npm pack is empty.')

if not isinstance(pkg_json, list):
raise ValueError('Expected npm pack output to be a JSON array.')

filename = pkg_json[0].get('filename')
if filename is None:
raise KeyError(
"Key 'filename' not found in the first element "
'of the JSON array.',
)
Copy link
Member

Choose a reason for hiding this comment

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

I would just let these raise

Comment on lines +116 to +117
def _make_hello_world(tmp_path, package_json=None):
package_json = package_json or '''\
Copy link
Member

Choose a reason for hiding this comment

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

rather than changing this function -- call it and then write a package.json in the specific test below

assert ret == (0, b'Hello World\n')


def test_node_with_prepare_script(tmp_path):
Copy link
Member

Choose a reason for hiding this comment

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

can you show the output of this failing before your patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants