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

build: include a source map in the packaged dist #32

Merged
merged 11 commits into from
Oct 1, 2024
Merged

Conversation

zimeg
Copy link
Member

@zimeg zimeg commented Aug 1, 2024

Summary

This PR adds an index.js.map to the build output to improve outputs when error debugging a failed action. Example:

/Users/runner/work/_actions/slackapi/slack-health-score/v0/dist/index.js:69097
    const commentType = (commentData == null) ? null : type.split('//')[1].trim();
                                                                          ^

TypeError: Cannot read properties of undefined (reading 'trim')

With this file added, that error should point to the actual line number 🙏

Reviewers

Ensure the build keeps this file:

$ npx ncc build src/index.js -o dist --license licenses.txt --source-map
$ ls dist
index.js
index.js.map
licenses.txt
sourcemap-register.js

$ cat dist/index.js.map  # Caution: Lots of lines
...
//# sourceMappingURL=index.js.map

Requirements

@zimeg zimeg added enhancement New feature or request semver:patch addressing/merging the change would necessitate a patch semver release labels Aug 1, 2024
@zimeg zimeg self-assigned this Aug 1, 2024
@filmaj
Copy link

filmaj commented Aug 1, 2024

How can we test that this works? 🤔

@zimeg
Copy link
Member Author

zimeg commented Aug 1, 2024

@filmaj I was hoping including the file in build outputs would be enough, but I'm not certain what gets included in tagged releases with the build and tag action... 😳

@filmaj
Copy link

filmaj commented Aug 1, 2024

OK I was able to sort of half-ass test this out and indeed it does work! I would suggest a couple minor changes as part of this PR while we're here:

Catch and report errors for health score compile

One change I had to make to see the difference in sourcemaps-present vs. not behaviour is that the main src/index.js file does not catch uncaught exceptions. So I had to make this change:

hs.compile(core, github)
  .catch((err) => { // <-- this part is new
    console.error('ERROR COMPILING SCORE!', err);
  })
  .then((score) => hs.report(startTime, core, github, score))

Not sure if this should be a simple console.error or perhaps it should be a core.error (or whatever the error-reporting GitHub Actions core package method is called)... but at least, to run locally and see the difference in stack traces, I had to use console.error.

The @returns JSDoc annotation for compile is wrong

Also had to change the @returns JSDoc for the compile method to:

   * @returns {Promise<import('./types').HealthScore>} score Health score details object

(the return result is a Promise - that is missing today)

Results

Once I had those two changes in place, then I could compare output/behaviour of this branch to that of main. To run against e.g. the slack-cli codebase, I would:

cd slack-health-score
git checkout main # or zimeg-build-source-map
rm -rf dist && npm run build
cd ../slack-cli
node
➜ node
Welcome to Node.js v20.14.0.
Type ".help" for more information.
> process.env.INPUT_INCLUDE = 'cmd internal'
'cmd internal'
> process.env.INPUT_EXTENSION = 'go'
'go'
> a=require('../slack-health-score/dist/index')

The output of main branch yields:

> ERROR COMPILING SCORE! TypeError: Cannot read properties of undefined (reading 'trim')
    at /Users/fmaj/src/slack-health-score/dist/index.js:69097:75
    at Array.map (<anonymous>)
    at Object.grepForProblematicComments [as grep] (/Users/fmaj/src/slack-health-score/dist/index.js:69092:53)
    at Object.compileScore [as compile] (/Users/fmaj/src/slack-health-score/dist/index.js:68827:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

.. that is, no source maps, stack trace references deep into built code. Yuck!

Whereas with your branch the output is:

> ERROR COMPILING SCORE! TypeError: Cannot read properties of undefined (reading 'trim')
    at /Users/fmaj/src/slack-health-score/src/score_components/find-problematic-comments.js:50:1
    at Array.map (<anonymous>)
    at Object.grepForProblematicComments [as grep] (/Users/fmaj/src/slack-health-score/src/score_components/find-problematic-comments.js:45:1)
    at Object.compileScore [as compile] (/Users/fmaj/src/slack-health-score/src/health-score.js:29:1)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

Much nicer!

Copy link

@filmaj filmaj left a comment

Choose a reason for hiding this comment

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

Overall great improvement but there needs to be a bit better error handling at least at the compile level as mentioned in my last comment. Likely need to examine what the appropriate way to raise errors using the actions/core package looks like as part of that error reporting change I requested.

Copy link

codecov bot commented Sep 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.48%. Comparing base (439f1cc) to head (e8867ba).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #32   +/-   ##
=======================================
  Coverage   95.48%   95.48%           
=======================================
  Files           6        6           
  Lines         332      332           
=======================================
  Hits          317      317           
  Misses         15       15           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zimeg
Copy link
Member Author

zimeg commented Sep 30, 2024

@filmaj Thanks for taking a look at this and the callouts and suggestions! 🙏 🙏

These are all great and I've made a few changes to the JSdoc and added the catch - that seems quite needed for this to be most useful 😉

I found that breaking the actual action is another way to test this in CI and that's showing as a run like this.

I'll revert the change that caused this failure, but the error message shown from that error is looking solid!

Copy link
Member Author

@zimeg zimeg left a comment

Choose a reason for hiding this comment

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

📝 Notes on changes for the kind reviewers

Comment on lines +9 to +12
.catch((err) => {
core.setFailed('Failed to check up on the health score!');
console.error(err);
});
Copy link
Member Author

Choose a reason for hiding this comment

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

Added the catch here at the end since I think hs.report was still attempted if the catch was before it. Open to adjustments though!

With the earlier catch
$ node
> healthscore=require('./dist/index')
...
::error::Failed to check up on the health score!
TypeError: core.getInpu is not a function
    at Object.compileScore [as compile] (/Users/ethan.zimbelman/programming/tools/slack-health-score/src/health-score.js:15:1)
    at /Users/ethan.zimbelman/programming/tools/slack-health-score/src/index.js:6:1
    at /Users/ethan.zimbelman/programming/tools/slack-health-score/dist/index.js:71959:3
    at Object.<anonymous> (/Users/ethan.zimbelman/programming/tools/slack-health-score/dist/index.js:71962:12)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1127:12)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
::warning::No GitHub token found; will not report score on commit status.
0
With the later catch
$ node
> healthscore=require('./dist/index')
...
::error::Failed to check up on the health score!
TypeError: core.getInpu is not a function
    at Object.compileScore [as compile] (/Users/ethan.zimbelman/programming/tools/slack-health-score/src/health-score.js:15:1)
    at /Users/ethan.zimbelman/programming/tools/slack-health-score/src/index.js:6:1
    at /Users/ethan.zimbelman/programming/tools/slack-health-score/dist/index.js:71959:3
    at Object.<anonymous> (/Users/ethan.zimbelman/programming/tools/slack-health-score/dist/index.js:71962:12)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1127:12)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)

@zimeg
Copy link
Member Author

zimeg commented Oct 1, 2024

@filmaj big thank yous to you 🙏

@zimeg zimeg merged commit 288e8c7 into main Oct 1, 2024
6 checks passed
@zimeg zimeg deleted the zimeg-build-source-map branch October 1, 2024 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request semver:patch addressing/merging the change would necessitate a patch semver release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants