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

Fix watch for packages when there are compilation errors in src/ #453

Merged
merged 2 commits into from
Oct 16, 2020

Conversation

lydell
Copy link
Collaborator

@lydell lydell commented Oct 15, 2020

Fixes #399.

Previously, if there was an error in src/ for a package project, elm-test --watch would immediately exit. This was because we compile src/ first to get .elmi files for the files in there so that elmi-to-json can find the exact dependencies there. But since #451 we don’t use elmi-to-json to find exact dependencies anymore – we use elm-json instead – so there’s no need for that compilation step anymore. I removed that, which fixes the issue and also improves test run time since the compiler is invoked one time less.

As for the undefined log issue, that was because of promises being rejected with strings while the .catch code expected Error objects. "some string".message is undefined, so that’s where it came from. I updated to reject with Error objects instead.

This allowed for some pretty nice cleanups.

: 'run `elm make` on ' + projectRootDir);

reject(msg);
reject(new Error(`\`elm make\` failed with exit code ${exitCode}.`));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The old error was super verbose and not useful when the nice error from elm make is printed just above anyway.

@lydell lydell force-pushed the fix-watch-package branch from 96b61be to 72bf805 Compare October 15, 2020 22:10
Copy link
Collaborator

@harrysarson harrysarson left a comment

Choose a reason for hiding this comment

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

Looking good. I always love to see patches with more lines removed than added.

projectElmJson = fs.readJsonSync(elmJsonPath);
} catch (err) {
console.error('Error reading elm.json: ' + err.message);
throw process.exit(1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we have to crash if we are in watch mode and the project elm json is invalid?

Copy link
Collaborator

Choose a reason for hiding this comment

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

And what happens if someone edits their elm.json while we are watching tests>

Copy link
Collaborator Author

@lydell lydell Oct 16, 2020

Choose a reason for hiding this comment

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

I didn’t think about that case! This could for sure be a future enhancement.

Currently this is done outside of the watch loop so it would require more changes than I’m comfortable with at the moment. This looks like added code but it’s in fact just moved so were keeping the same behavior as before regarding elm.json at least.

Currently, if elm.json changes while the tests run those changes are just ignored.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Before this PR generateElmJson read the elm.json file from disk everything. Would that mean that it would detect changes in your elm.json during watch mode?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, good catch, didn’t think about that change. I’ll have to investigate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Turns out Generate.generateElmJson is outside the watch loop too (and was before this PR as well). And moving it into the loop brings more issues:

  • We don’t watch elm.json so you need to edit some .elm file to trigger.
  • If you edit "source-directories" we need to recalculate which globs the watcher should look at, which is also outside the loop.
  • Reading elm.json and running elm-json currently does process.exit(1) on failure which would need to be refactored.
  • We basically need to restart the whole thing if elm.json changes.

My conclusion is:

  • This PR has not changed behavior.
  • Adding support for elm.json changes could be a nice future improvement.

Copy link
Collaborator

Choose a reason for hiding this comment

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

My conclusion is:

  • This PR has not changed behavior.

Good enough for me!

@harrysarson harrysarson merged commit cfb5402 into rtfeldman:master Oct 16, 2020
@lydell lydell deleted the fix-watch-package branch October 16, 2020 17:22
@lydell lydell mentioned this pull request Nov 21, 2020
harrysarson pushed a commit that referenced this pull request Dec 17, 2020
This makes the watcher faster and more stable! As a bonus, this made the code much more organized.

- Concentrate all `process.exit` in elm-test.js (except one in Install.js where I found no clean solution). Besides making the code easier to follow, it allows the watcher to continue running (and recover) even if there are errors.

- Don’t crash/stop running tests if files are removed.

- Pick up new Elm files added.

- Fixes #225

  We already had code for waiting for the previous run to finish before starting the next run, but it queued up every single file change/add/remove event and ran that many runs one after the other. Now, for all watch events during a run we run _once_ after the current run for all of them. We also sleep 100 ms at the start of each run to catch more events happening close by (such as when adding/removing a whole folder of tests). (Note: We used to sleep at least 500 ms via the [awaitWriteFinish](https://github.com/paulmillr/chokidar#performance) chokidar option.)

  I’ve also tested manually that running elm-format on save doesn’t cause double test runs: #194

- Fixes #355

  We now find the closest elm.json upwards in the folder hierarchy.

- Resolves #453 (comment)

  The watcher now listens for changes to elm.json and properly recalculates things. I thought that I would have to recalculate some things only when elm.json changes for performance, but after making the globbing faster there was no need so we simply rebuild everything on each run.

- Closes #463 and closes #464

  I can’t find any more places to use async/await.

- elm.json is now decoded properly in one place rather than here and there and just being `any`. We now have 0 explicit `any` type annotations!

- elm-test.js now only contains CLI logic and subcommand dispatching. The code for running tests have been moved to RunTests.js and to FindTests.js (which used to be called Runner.js).

- Improved .flowconfig that is stricter. Closes #478

- And lots of little code improvements along the way! I only touched things that I needed to change anyway in order to not make the diff balloon too much.

I’ve done some manual testing on Windows, Mac and Linux to make sure things work like they should.
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.

elm-test --watch crashes if Elm code doesn't compile
2 participants