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

[WIP/RFC] Warn when a fs call cannot be evaluated #587

Merged
merged 4 commits into from
Feb 5, 2018

Conversation

fathyb
Copy link
Contributor

@fathyb fathyb commented Jan 19, 2018

screen shot 2018-01-19 at 16 42 09

This PR will warn instead of throwing when a fs call cannot be evaluated.

Remarks :

  • I added a commit to create a logger singleton, we might want to wait for Logger singleton #462 to land first and then remove the commit.
  • It warns using a code frame, maybe it's too much, waiting for your feedback for that
  • The warning will not be propagated to the browser console
  • If the call cannot be evaluated then the fs module will be required as an empty module (Uncaught TypeError: fs.readFileSync is not a function on the runtime)
  • See the nodeNotEvaluated var in fs.js, I don't know if it's elegant
  • Should we handle the case where the filename is not a string? (eg. readFileSync({notAString: true}))

TODO:

@codecov-io
Copy link

codecov-io commented Jan 19, 2018

Codecov Report

Merging #587 into master will increase coverage by 4.9%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #587     +/-   ##
=========================================
+ Coverage   89.35%   94.25%   +4.9%     
=========================================
  Files          64       64             
  Lines        2019     2036     +17     
=========================================
+ Hits         1804     1919    +115     
+ Misses        215      117     -98
Impacted Files Coverage Δ
src/Logger.js 96.96% <66.66%> (+0.14%) ⬆️
src/WorkerFarm.js 93.22% <87.5%> (+0.23%) ⬆️
src/visitors/fs.js 87.5% <88.23%> (+1.97%) ⬆️
src/worker.js 90.9% <0%> (-9.1%) ⬇️
src/assets/CSSAsset.js 82.35% <0%> (-7.36%) ⬇️
src/utils/installPackage.js 93.75% <0%> (+3.12%) ⬆️
src/assets/LESSAsset.js 100% <0%> (+5%) ⬆️
src/assets/SASSAsset.js 100% <0%> (+5.26%) ⬆️
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fddfdb9...ee26483. Read the comment docs.

@fathyb fathyb force-pushed the fix/fs-call-eval branch 2 times, most recently from 6ba2512 to 188a202 Compare January 19, 2018 15:44
src/Logger.js Outdated
class Logger {
static forAsset(asset) {
Copy link
Member

Choose a reason for hiding this comment

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

Won't this bug the logger if multiple loggers exist?
Mainly like glitches with the status

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, this is just to quickly showcase the feature. A IPC system is way better like you did in the PR, like said in the description I'll remove this commit and use your PR once it gets merged.

However I'm not sure if Node.js scrambles the logs when stdout is shared across processes with child_process (it sure does when each stdout has it's own descriptor).

Copy link
Member

Choose a reason for hiding this comment

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

Ow alright, thought this was another way to implement a singleton, guess i misunderstood.
About the workers stdout, I think it's best if we either don't use it or catch it on the master process and redirect it to the logger to make sure everything is streamlined to prevent console glitches (If we do catch stdout, we don't need to run it over ipc, but we can actually use stdout as an interface for workers to use the logger)

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 more for just using the IPC and not messing too much around stdout (Windows + stdout + multiple processes don't blend well in my experience).

[bit off-topic]
And I really like the IPC idea, all my plugins are needing an IPC at some point and I ended up making my own using UNIX sockets. If we could streamline this and make it accessible to plugin developers it could really help.
[/bit off-topic]

Copy link
Member

Choose a reason for hiding this comment

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

Jasper's logger changes have been merged to master, so the IPC should just work now. You can just use the Logger singleton within the worker and things will be transparently proxied.

@@ -0,0 +1,5 @@
const dir = __dirname
Copy link
Contributor Author

@fathyb fathyb Jan 19, 2018

Choose a reason for hiding this comment

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

This is weird. This does not work (__dirname is not found by the globals visitor) :

module.exports = require('fs').readFileSync(__dirname + '/test.txt', {
  encoding: (typeof Date.now()).replace(/number/, 'utf-8')
})

While const dir = __dirname works. I think it may be a babylon-walk bug, but I'm not really sure about that. I checked the Identifier visitor and it is not called with the node, but it seems to be in the AST. I can't reproduce it outside of this test 😕

@@ -31,9 +37,37 @@ module.exports = {
__dirname: Path.dirname(asset.name),
__filename: asset.basename
};
let [filename, ...args] = path
.get('arguments')
.map(arg => evaluate(arg, vars));
Copy link
Member

Choose a reason for hiding this comment

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

Maybe instead of changing the logic to be a loop that exists early as below, you could just wrap this in a try...catch block and do the logging in the catch. That way you'd also not need the nodeNotEvaluated thing. Might clean up the code a bit.

src/Logger.js Outdated
class Logger {
static forAsset(asset) {
Copy link
Member

Choose a reason for hiding this comment

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

Jasper's logger changes have been merged to master, so the IPC should just work now. You can just use the Logger singleton within the worker and things will be transparently proxied.

@devongovett
Copy link
Member

This is awesome! The code frames are really nice I think. Definitely helps track down bugs. I could see how it might be annoying though if you know something cannot be evaluated on purpose (or in a node_module you don't own), and it keeps nagging you.

@fathyb fathyb force-pushed the fix/fs-call-eval branch 2 times, most recently from 765d6c4 to 63a80a0 Compare January 31, 2018 21:40
@fathyb
Copy link
Contributor Author

fathyb commented Jan 31, 2018

screen shot 2018-01-31 at 22 38 08

I pushed a new commit according to the review and added a code frame where fs.readFileSync throws.

@devongovett
Copy link
Member

Thanks! Cleaned it up just slightly to take advantage of the existing generateErrorMessage function on JSAsset. That will also provide a non-highlighted code frame for CLIs that don't support color.

Also needed to fix a couple things in WorkerFarm so that we don't log things when workers are warming up - caused duplicate messages to appear.

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

Successfully merging this pull request may close these issues.

None yet

4 participants