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

Compilation differs on GitHub Actions vs locally #831

Open
sethvargo opened this issue Dec 16, 2021 · 36 comments
Open

Compilation differs on GitHub Actions vs locally #831

sethvargo opened this issue Dec 16, 2021 · 36 comments
Labels
bug Something isn't working

Comments

@sethvargo
Copy link

When using the same version of ncc locally (OS X) and on GitHub Actions, the compiled file is slightly different (full diff):

diff --git a/dist/index.js b/dist/index.js
index 720bbbd..217b40a 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -55565,7 +55565,7 @@ module.exports = __nccwpck_require__(4083).YAML
 /***/ ((module) => {
 
 "use strict";
-module.exports = JSON.parse('{"license":"Apache-2.0"}');
+module.exports = JSON.parse('{"license":"Apache-2.0","_resolved":"https://registry.npmjs.org/google-auth-library/-/google-auth-library-7.10.4.tgz","_integrity":"sha512-DJ4VGaLwgPcMg8hMZFoQxjA0VeIy2A1RkJ6kUt5vXNfbVEma8WA9waUJ29cwpXjpFjpEAT2Uu6Y5loYbmInyFw==","_from":"google-auth-library@7.10.4"}');

This causes a perpetual diff between what's built locally and what's built by CI.

@styfle
Copy link
Member

styfle commented Dec 16, 2021

Are you using typescript? This sounds like it could be the same issue as #820

@sethvargo
Copy link
Author

We are using Typescript, but I don't think it's the same as #820, as there is a version property in the package.json.

Also, the build is succeeding, it just has different output when run on GitHub Actions vs when run locally.

@KhudaDad414
Copy link

KhudaDad414 commented Dec 17, 2021

same here. I am not using Typescript.
Local Machine: Windows
GitHub Actions: Ubuntu
GitHub Actions output:

Detected uncommitted changes after build.  See status below:
diff --git a/dist/index.js b/dist/index.js
index 24d024e..ce6d5ac 100644
Binary files a/dist/index.js and b/dist/index.js differ
Error: Process completed with exit code 1.

@styfle
Copy link
Member

styfle commented Dec 17, 2021

Are you using the same version of npm locally and in CI?

What about the same OS?

@sethvargo
Copy link
Author

Yes and same node version too. Deps are installed via "npm ci" and the lines in the compiled code are NOT in the package.json.

I've confirmed this my echoing the contents of package.json before "npm ci", after, and after "ncc". The contents never change and never include the "_resolved" references in the compiled js.

@rethab
Copy link
Contributor

rethab commented Jan 20, 2022

@sethvargo might it be this problem? #713

@sethvargo
Copy link
Author

I don't think so, because the difference only occurs with ncc - npm ci and npm install do not generate a different lock.

@rethab
Copy link
Contributor

rethab commented Jan 20, 2022

Fair enough. Do you have a reproducing sample for this? Looks like the branch you linked in the issue description is gone? (I can see the workflow logs, but not the branch it was run from)

@sethvargo
Copy link
Author

@rethab sure - I spent some time to remove all irrelevant code in my fork: https://github.com/sethvargo/get-gke-credentials/runs/4888123729. The repo is on main.

@rethab
Copy link
Contributor

rethab commented Jan 21, 2022

Hi @sethvargo , I have only very briefly checked your reproducer, but npm ci did make a difference when I tried so I would encourage you to revisit the issue I linked earlier.

I have forked your reproducer here: https://github.com/rethab/get-gke-credentials All I did to make it green was to run npm ci before npm run package and then commit the updated dist folder.


npm ci and npm install do not generate a different lock.

They don't generate a different package-lock.json in your project (in fact npm ci doesn't touch this file). However, what they do is they install packages differently. This results in different contents/structure in the node_modules folder. My analysis in #713 has more details on this.

Perhaps I'm wrong and you are indeed facing a different issue, but if you are facing the same issue, then here are two options I am aware of:

  • upgrade npm/node: actions should now support node 16 so you could probably use a newer version of npm where this inconsistency between npm ci and npm i doesn't exist
  • ensure you're running npm ci before committing: What we ended up doing is create an npm script all which essentially does: npm ci && npm test && npm package. So you just need to make sure you run npm run all before committing.

@sethvargo
Copy link
Author

sethvargo commented Jan 21, 2022

Hi @rethab

Thank you for looking into this. Running npm ci on my Mac produces no git diff. If I clone a fresh https://github.com/sethvargo/get-gke-credentials and run npm ci, there are no changes. Deleting the entire node_modules directory and running npm ci also doesn't generate a diff.

There is no npm run package step, so I'm not sure what you're referring to here. If you meant npm run build, deleting node_modules, running npm ci, and running npm run build also generates no diff. Per rethab/get-gke-credentials@73ece2d, there has to be some diff that's generated.

  • Mac OS 12.1 (21C52)
  • Node v12.22.5
  • NPM 8.3.0

Regarding using Node 16, actions/runner#1439 was merged, and released in https://github.com/actions/runner/releases/tag/v2.285.0, but it will also likely be a few months until it's available in GitHub Enterprise installations. A lot of our users use self-hosted runners with GitHub Enterprise, so we can't switch until it's made available everywhere.

@rethab
Copy link
Contributor

rethab commented Jan 21, 2022 via email

@sethvargo
Copy link
Author

Hi @rethab thanks for the reply. I think GitHub or your email client did something odd. The command came in as npx ***@***.*** ci

CleanShot 2022-01-21 at 09 47 41@2x

Don't worry about replying until you're back at a computer - this isn't a P0 since we have a workaround for now.

@rethab
Copy link
Contributor

rethab commented Jan 21, 2022 via email

@sethvargo
Copy link
Author

Welp, that seems to be the issue:

$ rm -rf node_modules && npx npm@6 ci && npx npm@6 run build
added 46 packages in 1.028s

> get-gke-credentials@0.5.0 build /Users/sethvargo/get-gke-credentials
> ncc build src/main.ts

ncc: Version 0.33.1
ncc: Compiling file index.js into CJS
ncc: Using typescript@4.5.5 (local user-provided)
1617kB  dist/index.js
1617kB  [3422ms] - ncc 0.33.1
➜  get-gke-credentials main ❃ git diff
diff --git a/dist/index.js b/dist/index.js
index 9e0f160..9535565 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -45624,7 +45624,7 @@ module.exports = require("zlib");
 /***/ ((module) => {

 "use strict";
-module.exports = JSON.parse('{"name":"google-auth-library","version":"7.11.0","author":"Google Inc.","description":"Google APIs Authentication Client Library for Node.js","engines":{"node":">=10"},"main":"./build/src/index.js","types":"./build/src/index.d.ts","repository":"googleapis/google-auth-library-nodejs.git","keywords":["google","api","google apis","client","client library"],"dependencies":{"arrify":"^2.0.0","base64-js":"^1.3.0","ecdsa-sig-formatter":"^1.0.11","fast-text-encoding":"^1.0.0","gaxios":"^4.0.0","gcp-metadata":"^4.2.0","gtoken":"^5.0.4","jws":"^4.0.0","lru-cache":"^6.0.0"},"devDependencies":{"@compodoc/compodoc":"^1.1.7","@types/base64-js":"^1.2.5","@types/chai":"^4.1.7","@types/jws":"^3.1.0","@types/lru-cache":"^5.0.0","@types/mocha":"^8.0.0","@types/mv":"^2.1.0","@types/ncp":"^2.0.1","@types/node":"^16.0.0","@types/sinon":"^10.0.0","@types/tmp":"^0.2.0","assert-rejects":"^1.0.0","c8":"^7.0.0","chai":"^4.2.0","codecov":"^3.0.2","execa":"^5.0.0","gts":"^2.0.0","is-docker":"^2.0.0","karma":"^6.0.0","karma-chrome-launcher":"^3.0.0","karma-coverage":"^2.0.0","karma-firefox-launcher":"^2.0.0","karma-mocha":"^2.0.0","karma-remap-coverage":"^0.1.5","karma-sourcemap-loader":"^0.3.7","karma-webpack":"^5.0.0","keypair":"^1.0.4","linkinator":"^2.0.0","mocha":"^8.0.0","mv":"^2.1.1","ncp":"^2.0.0","nock":"^13.0.0","null-loader":"^4.0.0","puppeteer":"^13.0.0","sinon":"^12.0.0","tmp":"^0.2.0","ts-loader":"^8.0.0","typescript":"^3.8.3","webpack":"^5.21.2","webpack-cli":"^4.0.0"},"files":["build/src","!build/src/**/*.map"],"scripts":{"test":"c8 mocha build/test","clean":"gts clean","prepare":"npm run compile","lint":"gts check","compile":"tsc -p .","fix":"gts fix","pretest":"npm run compile","docs":"compodoc src/","samples-setup":"cd samples/ && npm link ../ && npm run setup && cd ../","samples-test":"cd samples/ && npm link ../ && npm test && cd ../","system-test":"mocha build/system-test --timeout 60000","presystem-test":"npm run compile","webpack":"webpack","browser-test":"karma start","docs-test":"linkinator docs","predocs-test":"npm run docs","prelint":"cd samples; npm link ../; npm install","precompile":"gts clean"},"license":"Apache-2.0"}');
+module.exports = JSON.parse('{"name":"google-auth-library","version":"7.11.0","author":"Google Inc.","description":"Google APIs Authentication Client Library for Node.js","engines":{"node":">=10"},"main":"./build/src/index.js","types":"./build/src/index.d.ts","repository":"googleapis/google-auth-library-nodejs.git","keywords":["google","api","google apis","client","client library"],"dependencies":{"arrify":"^2.0.0","base64-js":"^1.3.0","ecdsa-sig-formatter":"^1.0.11","fast-text-encoding":"^1.0.0","gaxios":"^4.0.0","gcp-metadata":"^4.2.0","gtoken":"^5.0.4","jws":"^4.0.0","lru-cache":"^6.0.0"},"devDependencies":{"@compodoc/compodoc":"^1.1.7","@types/base64-js":"^1.2.5","@types/chai":"^4.1.7","@types/jws":"^3.1.0","@types/lru-cache":"^5.0.0","@types/mocha":"^8.0.0","@types/mv":"^2.1.0","@types/ncp":"^2.0.1","@types/node":"^16.0.0","@types/sinon":"^10.0.0","@types/tmp":"^0.2.0","assert-rejects":"^1.0.0","c8":"^7.0.0","chai":"^4.2.0","codecov":"^3.0.2","execa":"^5.0.0","gts":"^2.0.0","is-docker":"^2.0.0","karma":"^6.0.0","karma-chrome-launcher":"^3.0.0","karma-coverage":"^2.0.0","karma-firefox-launcher":"^2.0.0","karma-mocha":"^2.0.0","karma-remap-coverage":"^0.1.5","karma-sourcemap-loader":"^0.3.7","karma-webpack":"^5.0.0","keypair":"^1.0.4","linkinator":"^2.0.0","mocha":"^8.0.0","mv":"^2.1.1","ncp":"^2.0.0","nock":"^13.0.0","null-loader":"^4.0.0","puppeteer":"^13.0.0","sinon":"^12.0.0","tmp":"^0.2.0","ts-loader":"^8.0.0","typescript":"^3.8.3","webpack":"^5.21.2","webpack-cli":"^4.0.0"},"files":["build/src","!build/src/**/*.map"],"scripts":{"test":"c8 mocha build/test","clean":"gts clean","prepare":"npm run compile","lint":"gts check","compile":"tsc -p .","fix":"gts fix","pretest":"npm run compile","docs":"compodoc src/","samples-setup":"cd samples/ && npm link ../ && npm run setup && cd ../","samples-test":"cd samples/ && npm link ../ && npm test && cd ../","system-test":"mocha build/system-test --timeout 60000","presystem-test":"npm run compile","webpack":"webpack","browser-test":"karma start","docs-test":"linkinator docs","predocs-test":"npm run docs","prelint":"cd samples; npm link ../; npm install","precompile":"gts clean"},"license":"Apache-2.0","_resolved":"https://registry.npmjs.org/google-auth-library/-/google-auth-library-7.11.0.tgz","_integrity":"sha512-3S5jn2quRumvh9F/Ubf7GFrIq71HZ5a6vqosgdIu105kkk0WtSqc2jGCRqtWWOLRS8SX3AHACMOEDxhyWAQIcg==","_from":"google-auth-library@7.11.0"}');

 /***/ }),

So is the best option here to wait until node16 is more broadly available on Actions?

@rethab
Copy link
Contributor

rethab commented Jan 21, 2022 via email

@rickstaa
Copy link

I followed the steps provided by @rethab, but I am still experiencing #831 with node: v16.16.0, npm: v8.11.0 and ncc: Version 0.31.1(see https://github.com/rickstaa/top-issues-action/runs/7520924672?check_suite_focus=true). I also ensured I installed the same versions on my local PC as in the GitHub action (see https://github.com/rickstaa/top-issues-action/blob/main/.github/workflows/check-dist.yml), but the dist folder in the GitHub action is still different from my local version. Does anybody maybe have an idea why the difference check is failing? I got one successful run but can not find out what is different (see https://github.com/rickstaa/top-issues-action/runs/7519296142?check_suite_focus=true).

What I already tried

  1. Removing the node_modules and package-lock.json files.
  2. Installing the dependencies on my local PC using both the npm ci and npm i commands.
  3. Running both the npm build and npm package commands.

@rethab
Copy link
Contributor

rethab commented Jul 26, 2022

Can you please change the git diff to git diff --text such that I can see what the difference actually is?

@rickstaa
Copy link

@rethab Thanks for your fast answer. I now added a line showing the git diff --text (see https://github.com/rickstaa/top-issues-action/actions/runs/2740998372). I also included the dist folders from the repo and the action below:

dist_repo.zip
dist.zip

The only difference is between the index.js.map files.

@rethab
Copy link
Contributor

rethab commented Jul 27, 2022

The only difference is between the index.js.map files.

In your logs, I also see dist/index.js differ.

But if the map files are indeed the only ones that differ, why are you generating them in the first place? Please note that I never generate them and this issue is only about dist/index.js :)

@rickstaa
Copy link

rickstaa commented Jul 27, 2022

In your logs, I also see dist/index.js differ.

Yea, in the diff in the action, the dist/index.js is also said to differ. I think git diff is confused since it says an exact replica of the file replaced the whole file. I compared the files locally; they are exactly the same, even the return characters. I tried removing the dist folder and package-json.json to fix this, but the action keeps reporting differences that are not there (see https://github.com/rickstaa/top-issues-action/runs/7535091617?check_suite_focus=true). Kind of a mystery. Maybe there are some other hidden characters that I'm unaware of.

But if the map files are indeed the only ones that differ, why are you generating them in the first place? Please note that I never generate them and this issue is only about dist/index.js :)

They are generated because the template I used specified that. I now removed them as I do agree with you that, in this case, they should not be shipped in production.

Local files

dist_local.zip

Action files

dist_action.zip

@rickstaa
Copy link

After all, it seemed to be a line-ending problem, which was introduced by me working on both a Linux and Windows PC. After again changing the line endings and enabling git config core.autocrlf on windows, the problem seems to be fixed.

@Justin-JHG
Copy link

@rickstaa wondering how your one works, I dev on Windows and run action in Ubuntu and it always failing with the git diff for index.js.map file. I did manual compare, the local windows one has extra \r\ in it compare to the one in Ubuntu, and the following doesn't seem to ignore it :
if [ "$(git diff --ignore-cr-at-eol --ignore-space-at-eol dist/ | wc -l)" -gt "0" ]; then

@Justin-JHG
Copy link

I got the exact issue while dev on windows and run action on Linux

I fixed by udpating package.json as following:

so I added newLine option for tsc build
`

"scripts": {

"build": "tsc --newLine lf",
"format": "prettier --write src/**/*.ts",
"format-check": "prettier --check src/**/*.ts",
"lint": "eslint src/**/*.ts",
"package": "ncc build --source-map --no-cache --license licenses.txt",
"test": "jest --passWithNoTests",
"all": "npm run build && npm run format && npm run lint && npm run package && npm test"

},

@cryi
Copy link

cryi commented Mar 21, 2023

I had to add --external encoding to the ncc build. I wasn't able to figure out yet why it bundles in encoding on my desktop and not within github action. Node, npm, ncc everything uses same version 🤷🏻 But at least it works when I exclude encoding manually.

@IvanZosimov
Copy link

IvanZosimov commented Mar 23, 2023

Hi, all 👋 , in https://github.com/actions/setup-java repo we faced pretty the same issue. The result output of the build command is different on Windows and macOS/Linux. Here you can check the output .js files:
Windows: windows.zip
Linux: linux.zip
Output of the git diff command can be found here: gitDiff.zip

Runners specification:

Tool Ubuntu 22.04.1 Windows 11
TS 4.2.3 4.2.3
Node.js 16.19.0 16.19.1
NPM 8.19.3 8.19.3
NCC 0.33.4 0.33.4

If you open the git diff output you may notice that the innards of these files are identical (same number of lines, same blocks of code), only structure is different - some blocks of code are shifted.

We don't have such a problem in other repos where we use ncc, like https://github.com/actions/setup-node files built on different OS are identical. Could you give us a hint, what we are doing wrong? Can we even expect that the built files should be identical ?

@styfle styfle added the bug Something isn't working label Mar 23, 2023
@Rubilmax
Copy link

Rubilmax commented Jun 13, 2023

I had to add --external encoding to the ncc build. I wasn't able to figure out yet why it bundles in encoding on my desktop and not within github action. Node, npm, ncc everything uses same version 🤷🏻 But at least it works when I exclude encoding manually.

Thanks, but it didn't solve the issue for me: https://github.com/Rubilmax/foundry-gas-diff/actions/runs/5253244964/jobs/9490315554 (commit Rubilmax/foundry-gas-diff@69fea8e)

@Rubilmax
Copy link

Rubilmax commented Jun 13, 2023

It has to do something with GitHub actions runner OS because I haven't changed the version of ncc I use, and still my build went different

@SayakMukhopadhyay
Copy link

I think I have got the problem. Running tsc on Windows will generate the lib folder with everything in CRLF. Now, if ncc build --source-map is run, it will bundle the files in the lib folder, with the CRLF endings. To fix on my end, I manually updated the line endings of the files in lib and then executed ncc build ... and now GitHub actions doesn't show the problem.

Of course as per actions/typescript-action#528 (comment), the diff command must use --text to ensure that the diff recognises the files as text and not binary.

SayakMukhopadhyay added a commit to beezlabs-org/prevent-file-change that referenced this issue Jun 16, 2023
SayakMukhopadhyay added a commit to beezlabs-org/semantic-pull-requests that referenced this issue Jun 16, 2023
@Kurt-von-Laven
Copy link

We ran into the same issue in ScribeMD/docker-cache and were able to address it with yarn run ncc cache clean. Before clearing the cache, the behavior of yarn run ncc build src/main.ts --out dist/main --minify was not consistent even on my development laptop. Disabling minification was another, less desirable, workaround we found. I am curious whether clearing the ncc cache works for anyone else facing nondeterministic results.

@IvanZosimov
Copy link

IvanZosimov commented Jul 31, 2023

Hi, @Kurt-von-Laven, unfortunately, for us your solution didn't work out. For reference, #831 (comment)

@SayakMukhopadhyay
Copy link

@IvanZosimov could you try my method as mentioned in #831 (comment)

@Kurt-von-Laven
Copy link

Kurt-von-Laven commented Aug 5, 2023

@SayakMukhopadhyay, it sounds like there are separate bugs since we aren't using Windows at all.

@kevva
Copy link

kevva commented Sep 19, 2023

If you have a .gitattributes file in your project you can put * text=auto eol=lf at the top to always use LF line endings and it'll respect that.

@Kurt-von-Laven
Copy link

There are definitely at least two different bugs here then, because we already had equivalent settings in our .gitattributes when we encountered this issue.

@chriscarrollsmith
Copy link

I had to add --external encoding to the ncc build. I wasn't able to figure out yet why it bundles in encoding on my desktop and not within github action. Node, npm, ncc everything uses same version 🤷🏻 But at least it works when I exclude encoding manually.

Thank you so much. This resolved my issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests