Skip to content

chore(test): use import attributes for json files#3993

Merged
tido64 merged 1 commit intomainfrom
tido/test-import-manifest
Feb 25, 2026
Merged

chore(test): use import attributes for json files#3993
tido64 merged 1 commit intomainfrom
tido/test-import-manifest

Conversation

@tido64
Copy link
Member

@tido64 tido64 commented Feb 24, 2026

Description

This replaces a fs.readFileSync call with import(). It changes nothing, just makes things neater.

Test plan

n/a

@github-actions github-actions bot added the chore Improvements that don't directly affect features label Feb 24, 2026
// `ERR_UNKNOWN_FILE_EXTENSION` when encountering `.ts` files.
const version = Number(process.version.slice(1).split(".")[0]);
return version < 22 || manifest.includes('"type": "commonjs"');
return manifest.type === "commonjs";
Copy link
Collaborator

Choose a reason for hiding this comment

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

With the ability to run .ts in node directly do we still need tsx?

Copy link
Member Author

Choose a reason for hiding this comment

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

Last I tried, it failed on commonjs modules, which is why we have this check. But I can check again to make sure.

return manifest;
async function importManifest(cwd) {
const url = pathToFileURL(path.join(cwd, "package.json"));
const manifest = await import(url.href, { with: { type: "json" } });
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe the main tradeoff with using import vs. readFileSync is that with import the file is loaded into the module cache, though it remains a mutable object, meaning if you edit a field in the manifest it will be reflected everywhere you also import, but not if you read the file. This is generally great in normal situations, but doesn't work as well in situations where we would write package.json files (align-deps, yarn constraints, etc.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

require('./package.json') is the same as import in terms of module cache behavior. We should probably have a consistent philosophy of how we approach this in the repo. This change should be fine though, script command usage makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think in general, you'll want to read the file and then parse it to ensure no side effects. We use import here because it's more concise and the command basically exits in this scope.

@tido64 tido64 merged commit 910cca4 into main Feb 25, 2026
15 checks passed
@tido64 tido64 deleted the tido/test-import-manifest branch February 25, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Improvements that don't directly affect features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants