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

feat(@embark/test-runner): introduce artifacts.require API #2217

Merged
merged 1 commit into from
Feb 12, 2020

Conversation

0x-r4bbit
Copy link
Contributor

This commit adds a convenience API artifacts.require(name) that aims to make
requiring artifacts a little bit more straight forward.

Usage:

const SimpleStorage = artifacts.require('SimpleStorage');
const EmbarkJS = artifacts.require('EmbarkJS');

What did you refactor, implement, or fix?

How did you do it?

Is there anything that needs special attention (breaking changes, etc)?

Questions

Review

Cool Spaceship Picture

@ghost
Copy link

ghost commented Jan 24, 2020

DeepCode's analysis on #ea7f02 found:

  • 0 critical issues. ⚠️ 0 warnings and 1 minor issue. ✔️ 0 issues were fixed.

💬 This comment has been generated by the DeepCode bot, installed by the owner of the repository. The DeepCode bot protects your repository by detecting and commenting on security vulnerabilities or other critical issues.


☺️ If you want to provide feedback on our bot, here is how to contact us.

Copy link
Collaborator

@jrainville jrainville left a comment

Choose a reason for hiding this comment

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

Lol, we've come full circle

if (name === 'EmbarkJS') {
return EmbarkJS;
}
return require(`Embark/contracts/${name}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Embark is a special alias that's only setup in the context of a dapp when it's using the legacy webpack pipeline:

I'm confused re: how this is supposed to work because isn't mocha-tests something totally independent from the legacy pipeline?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @jrainville! I had forgotten about that, will unmark as "changes requested".

In the bigger scheme of things, I think this kind of patching is problematic, though it works for now and that's great. It's "magic" that goes against the idea of Embark being agnostic with respect to other tools, whether they be front-end build tools or test runners, etc. I think part of the future re-think of EmbarkJS needs to carefully consider the relationship between artifact/scaffolding generation and the range of ways users may need/want to import the generated modules: require, import, import().

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 "magic" that goes against the idea of Embark being agnostic with respect to other tools, whether they be front-end build tools or test runners, etc

@michaelsbradleyjr I fully agree with what you say. Have the same concerns and raised them a couple of times in the past. However it seems that for now the main focus it to ensure interoperability/easy migration with other tools.

Copy link
Contributor

@michaelsbradleyjr michaelsbradleyjr Jan 27, 2020

Choose a reason for hiding this comment

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

A few more thoughts on this, and maybe @jrainville can weigh in with respect to the relevant code in mocha-tests.

The use of "Embark/contracts/" in mocha-tests seems to have been for parity with the aliases setup in the legacy pipeline.

While removing the Module.prototype.require hack would be a breaking change — since dapps would no longer be able to manually require('Embark/contracts/[contract]') — maybe we have the opportunity to create a deprecation path.

What if an artifactsRequire function were created in an outer scope (or maybe as a static method on class MochaTestRunner). Then global.artifacts.require could simply be a reference to artifactsRequire; and Module.prototype.require could have logic that checks for Embark/contracts and Embark/EmbarkJS paths and in those cases call to artifactsRequire but warn the user that Embark/ paths are deprecated and in the future global artifacts.require() should be used instead.

Copy link
Collaborator

@jrainville jrainville Jan 27, 2020

Choose a reason for hiding this comment

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

It should be doable. I don't mind. Depends what @iurimatias has in mind.
iirc, in the 4.0 breaking changes, we had that we no longer supported magic requires, but then we added them back 🤷‍♂ (confirmed here: https://framework.embarklabs.io/docs/migrating_from_3.x.html#Updating-%E2%80%9Cmagic%E2%80%9D-EmbarkJS-imports)

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be clear. What I'm suggesting above is that since the magic requires are available to users currently, we shouldn't break them in v5. But we can move toward isolating them to the legacy webpack pipeline, since they're not in any way needed for a global artifacts.require to work as desired.

@0x-r4bbit 0x-r4bbit force-pushed the feat/artifact-require branch from 3df75f1 to 2d14a99 Compare January 27, 2020 09:31
@0x-r4bbit
Copy link
Contributor Author

Updated the PR to make CI happy. @michaelsbradleyjr I've also added an inline comment to avoid confusion for the next person looking at this code.

Thanks for the review!

@emizzle
Copy link
Collaborator

emizzle commented Feb 4, 2020

I agree that we should create a deprecation path for magic imports, however, I don't think that needs to be addressed in this PR as the scope of changes is larger (would affect dapps and tests, whereas this PR affects only tests) and more considerations needs to be taken in to account.

@michaelsbradleyjr
Copy link
Contributor

michaelsbradleyjr commented Feb 6, 2020

Here's a comparison between this PR's branch and a branch that implements what I proposed in earlier comments:

feat/artifact-require...feat/artifact-require-mb

In addition to changes in packages/plugins/mocha-tests/src/lib/index.js, the dapp templates and tests have been updated to use the artifacts.require() syntax.

If this PR were updated to include the changes I'm proposing and then included in Embark v5.2.0, when embark test is run in dapps still using the "magic prefix" requires (in tests), a deprecation warning will be printed. See the screenshot below (it's the demo modified to still use the prefix in its /test/simple_storage_spec.js):

image

In Embark v6.0.0 (or a later major version) we would then completely remove the monkey-patching of Module.prototype.require in packages/plugins/mocha-tests/src/lib/index.js.

Note that this deprecation path for the monkey-patched Module.prototype.require is completely independent of use of "magic prefixes" in dapp sources that are built using our legacy basic-pipeline. Case in point, the prefixes have not been removed from the import statements in .js files in dapps/templates/demo/app/ and they still work correctly.

@iurimatias
Copy link
Collaborator

IMO we should go with @michaelsbradleyjr changes

This commit adds a convenience API `artifacts.require(name)` that aims to make
requiring artifacts a little bit more straight forward.

Usage:

```
const SimpleStorage = artifacts.require('SimpleStorage');
const EmbarkJS = artifacts.require('EmbarkJS');
```
@0x-r4bbit 0x-r4bbit force-pushed the feat/artifact-require branch from 2d14a99 to ea7f020 Compare February 11, 2020 11:18
@0x-r4bbit
Copy link
Contributor Author

@iurimatias rebased this with @michaelsbradleyjr changes

@iurimatias iurimatias merged commit b021689 into master Feb 12, 2020
@iurimatias iurimatias deleted the feat/artifact-require branch February 12, 2020 19:17
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.

5 participants