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

RFC: Enable depending on NPM packages #8

Merged
merged 2 commits into from
Mar 21, 2019

Conversation

alexcrichton
Copy link
Contributor

@alexcrichton alexcrichton commented Feb 15, 2019

This proposal is an evolution of #4 which accounts for much of the discussion and makes a concrete proposal.

Rendered

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Thanks for this RFC, Alex. It provides answers for all of the open questions I had about the last iteration.

I have a few comments below, but these are nitpicks that I think we can largely address in implementation, and which don't need to block this RFC moving forward.

The primary goal of this RFC is to enable *tranparent* and *transitive*
dependencies on NPM. The `#[wasm_bindgen]` macro is the only aspect of a crate's
build which has access to all transitive dependencies, so this is what we'll be
using to slurp up `package.json`. When `#[wasm_bindgen]` with a `module` key is
Copy link
Member

Choose a reason for hiding this comment

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

Nice insight.

(note that the cwd is set by Cargo to be the directory with the crate's
`Cargo.toml` that is being compiled, or the crate in which `#[wasm_bindgen]` is
written). This `package.json`, if found, will have an absolute path to it
encoded into the custom section that `wasm-bindgen` already emits.
Copy link
Member

Choose a reason for hiding this comment

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

This dependency on package.json will be opaque to cargo -- does it make sense to always include the package.json path (even when it doesn't exist) in the custom section and have the CLI tool handle the missing case? This way incremental builds where a package.json is included post facto should always work.

I think modifications to package.json would work incrementally either way though, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually now that I think about it I think we can get rustc/cargo to learn about this dependency via include_str!. We could probably emit a dummy constant that doesn't end up getting used to force it to get included. That way the proc macro could actually work with the contents rather than just the path (and could put the entire contents in the custom section)

[alternatives]: #rationale-and-alternatives

When developing this RFC, some guiding values for its design have been
articulated:
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for summarizing all of this in one place.

@alexcrichton
Copy link
Contributor Author

alexcrichton commented Feb 15, 2019

I'd like to propose that we enter the Final Comment Period for this RFC. The original RFC has been around for quite some time and has baked for quite awhile, and I'd like to help spur on final discussion for this one before implementing!

Disposition: merge

@rustwasm/core members to sign off:

Copy link

@Pauan Pauan left a comment

Choose a reason for hiding this comment

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

I think this seems very reasonable for an MVP.

text/008-npm-dependencies.md Outdated Show resolved Hide resolved
}
```

The `package.json` file will initially be a subset of NPM's `package.json`,
Copy link

Choose a reason for hiding this comment

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

Are there any benefits to this restriction? Especially since we will support other fields in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm mostly thinking that we want to start out conservative. In the end wasm-bindgen will be producing a "merged" package.json, but that means that we in theory need a merging function for all fields of package.json. For now it's easy enough for us to define how to merge dependencies, but beyond that I figure we'd want to make explicit decisions about how to merge other fields rather than having something accidentally ad-hoc-ly define how to do it for us

Copy link

@Pauan Pauan Feb 19, 2019

Choose a reason for hiding this comment

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

Could we somehow error for transitive crates but not the current crate?

Since the current crate might be published to npm, so it might want things like name, version, etc.

But transitive crates aren't intended for publishing to npm, so it's okay to restrict them to dependencies only.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's possible I think! My hope though is that only allowing dependencies is a useful-enough MVP to build on a feature like this in the future. Do you think, though, that we need to allow other fields (in some possible form) in the initial pass to be useful enough though?

Copy link

Choose a reason for hiding this comment

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

I suspect that at least version will be needed, since the npm version can be different from the Rust version (e.g. breaking changes in .rs but not in compiled .wasm).

Maybe not needed for the MVP though, so I don't think it should block anything. I think this RFC is fine as-is.

@alexcrichton
Copy link
Contributor Author

ping @ashleygwilliams, have you had a chance to take a look at this and review the proposal for FCP in the past two weeks?

alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this pull request Feb 28, 2019
This commit implements [RFC 8], which enables transitive and transparent
dependencies on NPM. The `module` attribute, when seen and not part of a
local JS snippet, triggers detection of a `package.json` next to
`Cargo.toml`. If found it will cause the `wasm-bindgen` CLI tool to load
and parse the `package.json` within each crate and then create a merged
`package.json` at the end.

[RFC 8]: rustwasm/rfcs#8
@alexcrichton
Copy link
Contributor Author

I've got a sample implementation of the currently proposed version of the RFC at rustwasm/wasm-bindgen#1305

@alexcrichton
Copy link
Contributor Author

@ashleygwilliams another ping since last week to check the FCP proposal

@alexcrichton
Copy link
Contributor Author

@ashleygwilliams just another ping to check out the FCP proposal, would be great if we could get this in the near future!

@chinedufn
Copy link

@alexcrichton not to speak for Ashley but I noticed from twitter that she's recently moved cross country and changed jobs so I would imagine that she's super busy.

Not my intention to speak for her - just letting you know in case you don't already!

(@ashleygwilliams I'm really not trying to speak for you and just trying to call out that you might be a bit busy right now so feel free to let me know if I'm totally wrong!!!)

@alexcrichton
Copy link
Contributor Author

@chinedufn true! I'm trying to only ping at most once a week though because I want to make sure this doesn't fall off the radar but it also isn't too overwhelming. We currently require a full-team signoff (although that may change in #9), so we're unfortunately stuck leaving out this feature (which I personally feel is very important!) until @ashleygwilliams checks her box.

@chinedufn
Copy link

Sounds good - hadn't seen #9 - thanks!

@alexcrichton
Copy link
Contributor Author

🔔 🔔 🔔

This RFC has entered its final comment period. In seven calendar days, assuming no substantial new arguments or ideas are raised, we will merge it.

@fitzgen fitzgen merged commit 439fe1c into rustwasm:master Mar 21, 2019
alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this pull request Mar 22, 2019
This commit implements [RFC 8], which enables transitive and transparent
dependencies on NPM. The `module` attribute, when seen and not part of a
local JS snippet, triggers detection of a `package.json` next to
`Cargo.toml`. If found it will cause the `wasm-bindgen` CLI tool to load
and parse the `package.json` within each crate and then create a merged
`package.json` at the end.

[RFC 8]: rustwasm/rfcs#8
alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this pull request Mar 22, 2019
This commit implements [RFC 8], which enables transitive and transparent
dependencies on NPM. The `module` attribute, when seen and not part of a
local JS snippet, triggers detection of a `package.json` next to
`Cargo.toml`. If found it will cause the `wasm-bindgen` CLI tool to load
and parse the `package.json` within each crate and then create a merged
`package.json` at the end.

[RFC 8]: rustwasm/rfcs#8
@trivigy
Copy link

trivigy commented Dec 11, 2019

@alexcrichton The restriction just broke the builds! As part of the workflow scripts are included in package.json. In particular, when using webpack to do extra automation for scss, css, and a bunch of other bundling as well as prettier, and storybooks, and more. To execute on all of them and more there is a need for something like this:

{
  "scripts": {
    "clean": "rm -rf ./pkg ./target ./dist ./node_modules",
    "build": "webpack",
    "serve": "webpack-dev-server",
    "storybook": "start-storybook -p 6006",
    "format": "prettier --write \"./src/**/*.{js,scss}\""
  },
  "devDependencies": {
    "@babel/core": "^7.7.4",
    "@babel/plugin-syntax-dynamic-import": "^7.7.4",
    "@storybook/addon-actions": "^5.2.8",
    "@storybook/addon-knobs": "^5.2.8",
    "@storybook/addons": "^5.2.8",
    "@storybook/html": "^5.2.8",
    "@wasm-tool/wasm-pack-plugin": "^1.0.1",
    "babel-loader": "^8.0.6",
    "babel-plugin-macros": "^2.7.1",
    "clean-webpack-plugin": "^3.0.0",
    "copy-webpack-plugin": "^5.0.4",
    "css-loader": "^3.2.0",
    "fibers": "^4.0.2",
    "html-webpack-plugin": "^3.2.0",
    "paths.macro": "^2.0.2",
    "prettier": "^1.19.1",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "sass": "^1.23.1",
    "sass-loader": "^8.0.0",
    "style-loader": "^1.0.0",
    "ulid": "^2.3.0",
    "webpack": "^4.29.4",
    "webpack-cli": "^3.1.1",
    "webpack-dev-server": "^3.1.0"
  }
  }
}

In addition. Please note, that the dependencies these tools require are "devDependencies" only. I wouldn't want them to be included in the "dependencies" key in the first place. Also important to note here the use of @wasm-tool/wasm-pack-plugin. This one is a must because it is the only way to integrate into the storybook custom build process. They support an additional webpack.config.js file placed inside of .storybook directory.

Not entirely sure how I could work around such limitation of "only dependencies". Would appreciate some input.

@trivigy
Copy link

trivigy commented Dec 11, 2019

On a separate note, this RFC is extremely nuanced because the only reason this issue surfaced in my specific use case was due to the use of #[wasm_bindgen(module = "foo")] module dependency. Without in, there is no lookup and merging of the package.json and therefore this change was never noticed until now.

@alexcrichton
Copy link
Contributor Author

Sorry for the difficulties you're having @trivigy! I think that unfortunately there's not a great answer today, but brainstorming on how to solve this would be greatly appreciated!

@trivigy
Copy link

trivigy commented Dec 12, 2019

@alexcrichton I ended up spending the time rewriting everything in terms of inline_js. That's my work around for now. But I bet this exact issue will come up for others as well. Thanks for the hard work you put in and for responding.

@DarthGandalf
Copy link

This broke me too. I use npm install to install npm dependencies, followed by npm run build which uses webpack which invokes wasm-pack via @wasm-tool/wasm-pack-plugin. Can there be an option for wasm-pack to allow any keys in package.json and not require user to remove all keys from it which would break other tools which read it?

There are lots of tools which use package.json other than the dependencies key:

While some of them have alternative ways to configure them, breaking a common practice of using package.json for the sake of better integration with NPM ecosystem is self-contradicting.

Please remove this restriction and simply ignore unknown keys.

@alexcrichton
Copy link
Contributor Author

@DarthGandalf at this point it's probably best to open an issue and/or PR on wasm-bindgen, and please feel free to do so!

@Pauan
Copy link

Pauan commented Dec 16, 2019

I've given some thought to this, and I agree that we should allow for all keys in package.json. This makes it possible to use 1 package.json file (rather than 2), and it also matches what npm itself does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants