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

module: add --package flag for implicit config testing #38028

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bmeck
Copy link
Member

@bmeck bmeck commented Apr 1, 2021

refs: #37857

@bmeck bmeck added module Issues and PRs related to the module subsystem. esm Issues and PRs related to the ECMAScript Modules implementation. labels Apr 1, 2021
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Apr 1, 2021
Copy link
Contributor

@DerekNonGeneric DerekNonGeneric left a comment

Choose a reason for hiding this comment

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

Hmm, --package really? This is technically called a “package manifest” file while “package” more broadly refers to all of the package contents as well. Can we name this flag something else?

} catch {}
}
const implicitPackageJSONPath = getOptionValue('--package');
Copy link
Member

Choose a reason for hiding this comment

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

Yeah I agree with @DerekNonGeneric probably package-json-file or similar?

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'd prefer not package-json since the target could be named something like module-package.test we could go for package-file, does that seem fine?

Copy link
Contributor

@DerekNonGeneric DerekNonGeneric Apr 1, 2021

Choose a reason for hiding this comment

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

I think --package-config would be preferable since that is what it is called elsewhere.

E('ERR_INVALID_PACKAGE_CONFIG', (path, base, message) => {

Copy link
Contributor

Choose a reason for hiding this comment

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

How about --package-path and then also making it optional to include the package.json part?

Copy link
Member Author

Choose a reason for hiding this comment

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

@guybedford I'm not liking the excluding of the package.json since it is part of the path and we don't do searching generally these days. Additionally if we are fully explicit I wonder if we could do a inline-src like @aduh95 mentioned in #37848 (comment) for other stuff. If we do searching having it map directly to a URL would be weirder.

typeof cwdPackage.string === 'string' ||
typeof entryPackage.string === 'string'
) {
throw ERR_PACKAGE_NOT_ALLOWED();
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 like a reasonable tradeoff for now.

@bmeck
Copy link
Member Author

bmeck commented Apr 1, 2021

It isn't called a manifest anywhere in Node.js core to my knowledge. We generally call it a config.

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

This is obviously missing a bunch of stuff (tests/docs) but I think this is a good enabler for the use case mentioned in #37857 while addressing most concerns so +1 on the solution.

Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

Seems useful!

* @param {string} jsonPath
* @param {string} srcPath
*/
function clobber(jsonPath, srcPath) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about just making this set, and then providing the direct JSON to set into the cache? Since read is called before the usage of clobber anyway. This then also opens the door to possible inline package configuration options.

Copy link
Member Author

Choose a reason for hiding this comment

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

inline package configuration??

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah! (eg node app.js --pcfg.type=module as a natural extension)

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, i was imagining just allowing a source text for the file since manually configuring multiple of them would be noise for the args parser

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I wouldn't want to speculate, suggestion was as much about read + clobber = read + read + write -> read + write being the simplification.

} catch {}
}
const implicitPackageJSONPath = getOptionValue('--package');
Copy link
Contributor

Choose a reason for hiding this comment

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

How about --package-path and then also making it optional to include the package.json part?

@bmeck
Copy link
Member Author

bmeck commented Apr 1, 2021

looks like we have some naming ideas floating around, perhaps we should try and identify the components and what they bring:

  • package, the flag relates to a package (the entry package)

  • json, the flag expects to be provided JSON

It sure does, I doubt we will ever support anything else.

  • path, the value it takes is a path

This might not always be true, if we did allow URLs it would be possible to have inline sources / non-paths

  • config, the value represents the config for the package.

Overall I like --package-config the most, it is terse and doesn't bring up questions about having more types (URL w/ body vs just a file path) of values nor does it bring up questions about what would happen if we support non-JSON. Likely though, we should do an eager check of the JSON and error if this flag is provided. Right now malformed package.json files are ignored it seems after some checking, and we could match that but that could lead to misbehavior when attempting to ensure some kind of configuration with this flag I would think.

@ljharb
Copy link
Member

ljharb commented Apr 1, 2021

So, to be clear, the intention is that this flag accepts "package.json contents", and only works when no other package.json contents would be available?

I don't think anyone calls package.json "config" in the ecosystem, it's just called "package.json". The file serves many purposes; it's a dependency manifest, it's config, it's metadata, etc.

Why not --package-json= or --package-json-contents=?

@bmeck
Copy link
Member Author

bmeck commented Apr 1, 2021

So, to be clear, the intention is that this flag accepts "package.json contents", and only works when no other package.json contents would be available?

Yup, that is what the discussion thread referenced seemed to lean towards as an overall potential solution to the issue.

@jasnell
Copy link
Member

jasnell commented Apr 1, 2021

Seems like a good idea. Obviously there's some bikeshedding, docs, and tests needed but I'm +1 on the idea.

@GeoffreyBooth
Copy link
Member

cc @nodejs/modules

@bmeck
Copy link
Member Author

bmeck commented Apr 5, 2021

@ljharb

--package-json

I think this would be the first flag that declares its format in its name, not directly opposed since it likely will never ever change but it seems inconsistent. I think it is somewhat fine but might lead people to thinking it only works with files named package.json

--package-json-contents

This one likely isn't a good idea to me. In particular if it states "contents" I would expect the value to be the contents and not a path to get the contents from.

@bmeck
Copy link
Member Author

bmeck commented Apr 5, 2021

How do people feel about allowing this to be a URL (not talking HTTP support) so that data: works? This would be the first path parameter that takes a URL value. URLs do have an overlap with file paths so dealing with things like percent encoding get a bit tricky and wouldn't match --require or the main entry point resolver.

@ljharb
Copy link
Member

ljharb commented Apr 5, 2021

Ah, see, i misunderstood then :-) i thought it was passing json on the command line. Isn’t the whole point of the flag to not have to make a separate file solely to add type:module?

As for making it a url, that seems very strange. If we want the contents of the file to be passed inline, why not allow that directly?

@aduh95
Copy link
Contributor

aduh95 commented Apr 5, 2021

For reference, loaders are loaded by a URL rather than a path, which allows inlining:

node --experimental-loader 'data:text/javascript,export async function getFormat() { return { format: "module" } }' ./index.js

@bmeck
Copy link
Member Author

bmeck commented Apr 5, 2021

@ljharb the discussion around the need for this feature is for testing purposes, it has static configuration of those package.json contents so I presumed the ability to pass files would be nicer than always accepting JSON, also command lines limit total characters per command usually in different OS, so a full dump of a package.json could be problematic

@bmeck
Copy link
Member Author

bmeck commented Apr 5, 2021

@aduh95 I rather like that vs having 2 flags that take 2 different types which is why I think it could be useful to use here. Did you have an opinion on using a URL vs a file path?

@aduh95
Copy link
Contributor

aduh95 commented Apr 5, 2021

@bmeck my feeling is using URLs is more future proof – as you said, HTTP support is not on the table, but maybe one day. I agree that we should try to make only one flag for this. That being I don't feel strongly either way, and I'd like to hear other's opinions :)

@bmeck
Copy link
Member Author

bmeck commented May 27, 2021

I've been trying to get in consistent contact with TS about this but have failed except some indirect talk. Going to start moving forward w/o their feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. esm Issues and PRs related to the ECMAScript Modules implementation. lib / src Issues and PRs related to general changes in the lib or src directory. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants