ta2edchimp / opt-cli

Execute CLI Statements based upon Opt-In / Opt-Out Rules.
MIT License
21 stars 2 forks source link

fix(bin): Filter env null values passed to spawn #213

Closed sudo-suhas closed 7 years ago

sudo-suhas commented 7 years ago

I was facing issues with pre-commit hook while trying to contribute to cross-env. I initially thought that this was an issue with nps(kentcdodds/nps#155).

$ cat .opt-in
commit-msg
pre-commit

$ touch test-precommit.txt && git add test-precommit.txt

$ git commit

> husky - npm run -s precommit
> husky - node v8.2.1

25l[08:43:26] Running tasks for *.js [started]
[08:43:27] Running tasks for *.js [skipped]
[08:43:27] β†’ No staged files match *.js
25h25h
> husky - pre-commit hook failed (add --no-verify to bypass)
> husky - to debug, use 'npm run precommit'

By turning off the -s flag in husky pre-commit hook, I was able to get more info on the error:

npm error report ``` $ git commit husky > npm run -s precommit (node v8.2.1) > cross-env@0.0.0-semantically-released precommit E:\Projects\repos\cross-env-copy > lint-staged && opt --in pre-commit --exec "npm start validate" 25l[21:27:42] Running tasks for *.js [started] [21:27:42] Running tasks for *.js [skipped] [21:27:42] β†’ No staged files match *.js 25h25hnpm WARN invalid config access=undefined npm WARN invalid config also=undefined npm WARN invalid config https-proxy=undefined npm WARN invalid config Must be a full url with 'http://' npm WARN invalid config init-author-url=undefined npm WARN invalid config Must be a full url with 'http://' npm WARN invalid config only=undefined npm WARN invalid config proxy=undefined npm WARN invalid config Must be a full url with 'http://' npm WARN onload-script failed to require onload script undefined npm WARN onload-script Error: Cannot find module 'undefined' npm WARN onload-script at Function.Module._resolveFilename (module.js:485:15) npm WARN onload-script at Function.Module._load (module.js:437:25) npm WARN onload-script at Module.require (module.js:513:17) npm WARN onload-script at require (internal/module.js:11:18) npm WARN onload-script at cb (C:\nvm\v8.2.1\node_modules\npm\lib\npm.js:225:11) npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\npm.js:356:16 npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\config\core.js:81:7 npm WARN onload-script at Array.forEach (native) npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\config\core.js:80:13 npm WARN onload-script at f (C:\nvm\v8.2.1\node_modules\npm\node_modules\once\once.js:25:25) npm WARN onload-script { Error: Cannot find module 'undefined' npm WARN onload-script at Function.Module._resolveFilename (module.js:485:15) npm WARN onload-script at Function.Module._load (module.js:437:25) npm WARN onload-script at Module.require (module.js:513:17) npm WARN onload-script at require (internal/module.js:11:18) npm WARN onload-script at cb (C:\nvm\v8.2.1\node_modules\npm\lib\npm.js:225:11) npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\npm.js:356:16 npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\config\core.js:81:7 npm WARN onload-script at Array.forEach (native) npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\config\core.js:80:13 npm WARN onload-script at f (C:\nvm\v8.2.1\node_modules\npm\node_modules\once\once.js:25:25) npm WARN onload-script stack: 'Error: Cannot find module \'undefined\'\n at Function.Module._resolveFilename (module.js:485:15)\n at Function.Module._load (module.js:437:25)\n at Module.require (module.js:513:17)\n at require (internal/module.js:11:18)\n at cb (C:\\nvm\\v8.2.1\\node_modules\\npm\\lib\\npm.js:225:11)\n at C:\\nvm\\v8.2.1\\node_modules\\npm\\lib\\npm.js:356:16\n at C:\\nvm\\v8.2.1\\node_modules\\npm\\lib\\config\\core.js:81:7\n at Array.forEach (native)\n at C:\\nvm\\v8.2.1\\node_modules\\npm\\lib\\config\\core.js:80:13\n at f (C:\\nvm\\v8.2.1\\node_modules\\npm\\node_modules\\once\\once.js:25:25)', npm WARN onload-script code: 'MODULE_NOT_FOUND' } npm WARN invalid config access=undefined npm WARN invalid config also=undefined npm WARN invalid config https-proxy=undefined npm WARN invalid config Must be a full url with 'http://' npm WARN invalid config init-author-url=undefined npm WARN invalid config Must be a full url with 'http://' npm WARN invalid config only=undefined npm WARN invalid config proxy=undefined npm WARN invalid config Must be a full url with 'http://' npm WARN onload-script failed to require onload script undefined npm WARN onload-script Error: Cannot find module 'undefined' npm WARN onload-script at Function.Module._resolveFilename (module.js:485:15) npm WARN onload-script at Function.Module._load (module.js:437:25) npm WARN onload-script at Module.require (module.js:513:17) npm WARN onload-script at require (internal/module.js:11:18) npm WARN onload-script at cb (C:\nvm\v8.2.1\node_modules\npm\lib\npm.js:225:11) npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\npm.js:356:16 npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\config\core.js:81:7 npm WARN onload-script at Array.forEach (native) npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\config\core.js:80:13 npm WARN onload-script at f (C:\nvm\v8.2.1\node_modules\npm\node_modules\once\once.js:25:25) npm WARN onload-script { Error: Cannot find module 'undefined' npm WARN onload-script at Function.Module._resolveFilename (module.js:485:15) npm WARN onload-script at Function.Module._load (module.js:437:25) npm WARN onload-script at Module.require (module.js:513:17) npm WARN onload-script at require (internal/module.js:11:18) npm WARN onload-script at cb (C:\nvm\v8.2.1\node_modules\npm\lib\npm.js:225:11) npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\npm.js:356:16 npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\config\core.js:81:7 npm WARN onload-script at Array.forEach (native) npm WARN onload-script at C:\nvm\v8.2.1\node_modules\npm\lib\config\core.js:80:13 npm WARN onload-script at f (C:\nvm\v8.2.1\node_modules\npm\node_modules\once\once.js:25:25) npm WARN onload-script stack: 'Error: Cannot find module \'undefined\'\n at Function.Module._resolveFilename (module.js:485:15)\n at Function.Module._load (module.js:437:25)\n at Module.require (module.js:513:17)\n at require (internal/module.js:11:18)\n at cb (C:\\nvm\\v8.2.1\\node_modules\\npm\\lib\\npm.js:225:11)\n at C:\\nvm\\v8.2.1\\node_modules\\npm\\lib\\npm.js:356:16\n at C:\\nvm\\v8.2.1\\node_modules\\npm\\lib\\config\\core.js:81:7\n at Array.forEach (native)\n at C:\\nvm\\v8.2.1\\node_modules\\npm\\lib\\config\\core.js:80:13\n at f (C:\\nvm\\v8.2.1\\node_modules\\npm\\node_modules\\once\\once.js:25:25)', npm WARN onload-script code: 'MODULE_NOT_FOUND' } > cross-env@0.0.0-semantically-released start E:\Projects\repos\cross-env-copy > nps "validate" npm ERR! file undefined npm ERR! path undefined npm ERR! code ELIFECYCLE npm ERR! errno ENOENT npm ERR! syscall spawn undefined npm ERR! cross-env@0.0.0-semantically-released start: `nps "validate"` npm ERR! spawn undefined ENOENT npm ERR! npm ERR! Failed at the cross-env@0.0.0-semantically-released start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! code ELIFECYCLE npm ERR! errno -4058 npm ERR! cross-env@0.0.0-semantically-released start: `nps "validate"` npm ERR! Exit status -4058 npm ERR! npm ERR! Failed at the cross-env@0.0.0-semantically-released start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\suhas.karanth\AppData\Roaming\npm-cache\_logs\2017-08-13T15_57_45_556Z-debug.log npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! cross-env@0.0.0-semantically-released precommit: `lint-staged && opt --in pre-commit --exec "npm start validate"` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the cross-env@0.0.0-semantically-released precommit script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\suhas.karanth\AppData\Roaming\npm-cache\_logs\2017-08-13T15_57_45_809Z-debug.log husky > pre-commit hook failed (add --no-verify to bypass) ```


I also logged the process.env:

env variables ``` { '...': '...', npm_config_access: undefined, npm_config_allow_same_version: undefined, npm_config_also: undefined, npm_config_always_auth: undefined, npm_config_argv: '{"remain":[],"cooked":["run","precommit"],"original":["run","precommit"]}', npm_config_auth_type: 'legacy', npm_config_bin_links: 'true', npm_config_browser: undefined, npm_config_ca: undefined, npm_config_cache: 'C:\\Users\\********\\AppData\\Roaming\\npm-cache', npm_config_cache_lock_retries: '10', npm_config_cache_lock_stale: '60000', npm_config_cache_lock_wait: '10000', npm_config_cache_max: 'Infinity', npm_config_cache_min: '10', npm_config_cafile: undefined, npm_config_cert: undefined, npm_config_color: 'true', npm_config_depth: 'Infinity', npm_config_description: 'true', npm_config_dev: undefined, npm_config_dry_run: undefined, npm_config_editor: 'notepad.exe', npm_config_engine_strict: undefined, npm_config_fetch_retries: '2', npm_config_fetch_retry_factor: '10', npm_config_fetch_retry_maxtimeout: '60000', npm_config_fetch_retry_mintimeout: '10000', npm_config_force: undefined, npm_config_git: 'git', npm_config_git_tag_version: 'true', npm_config_global: undefined, npm_config_globalconfig: 'C:\\Users\\********\\AppData\\Roaming\\npm\\etc\\npmrc', npm_config_globalignorefile: 'C:\\Users\\********\\AppData\\Roaming\\npm\\etc\\npmignore', npm_config_global_style: undefined, npm_config_group: undefined, npm_config_ham_it_up: undefined, npm_config_heading: 'npm', npm_config_https_proxy: undefined, npm_config_if_present: undefined, npm_config_ignore_prepublish: undefined, npm_config_ignore_scripts: undefined, npm_config_init_author_email: undefined, npm_config_init_author_name: undefined, npm_config_init_author_url: undefined, npm_config_init_license: 'ISC', npm_config_init_module: 'C:\\Users\\********\\.npm-init.js', npm_config_init_version: '1.0.0', npm_config_json: undefined, npm_config_key: undefined, npm_config_legacy_bundling: undefined, npm_config_link: undefined, npm_config_local_address: undefined, npm_config_loglevel: 'notice', npm_config_logs_max: '10', npm_config_long: undefined, npm_config_maxsockets: '50', npm_config_message: '%s', npm_config_metrics_registry: 'http://registry.npmjs.org/', npm_config_msvs_version: '2015', npm_config_node_version: '8.2.1', npm_config_offline: undefined, npm_config_onload_script: undefined, npm_config_only: undefined, npm_config_optional: 'true', npm_config_package_lock: 'true', npm_config_parseable: undefined, npm_config_prefer_offline: undefined, npm_config_prefer_online: undefined, npm_config_prefix: 'C:\\Users\\********\\AppData\\Roaming\\npm', npm_config_production: undefined, npm_config_progress: 'true', npm_config_proprietary_attribs: 'true', npm_config_proxy: undefined, npm_config_python: 'C:\\Users\\********\\.windows-build-tools\\python27\\python.exe', npm_config_rebuild_bundle: 'true', npm_config_registry: 'http://registry.npmjs.org/', npm_config_rollback: 'true', npm_config_save: 'true', npm_config_save_bundle: undefined, npm_config_save_dev: undefined, npm_config_save_exact: 'true', npm_config_save_optional: undefined, npm_config_save_prefix: '^', npm_config_save_prod: undefined, npm_config_scope: undefined, npm_config_scripts_prepend_node_path: 'warn-only', npm_config_script_shell: undefined, npm_config_searchexclude: undefined, npm_config_searchlimit: '20', npm_config_searchopts: undefined, npm_config_searchstaleness: '900', npm_config_send_metrics: undefined, npm_config_shell: 'C:\\Windows\\system32\\cmd.exe', npm_config_shrinkwrap: 'true', npm_config_sign_git_tag: undefined, npm_config_sso_poll_frequency: '500', npm_config_sso_type: 'oauth', npm_config_strict_ssl: 'true', npm_config_tag: 'latest', npm_config_tag_version_prefix: 'v', npm_config_timing: undefined, npm_config_tmp: 'C:\\Users\\********\\AppData\\Local\\Temp', npm_config_umask: '0000', npm_config_unicode: undefined, npm_config_unsafe_perm: 'true', npm_config_usage: undefined, npm_config_user: undefined, npm_config_userconfig: 'C:\\Users\\********\\.npmrc', npm_config_user_agent: 'npm/5.3.0 node/v8.2.1 win32 x64', npm_config_version: undefined, npm_config_versions: undefined, npm_config_viewer: 'browser', npm_execpath: 'C:\\nvm\\v8.2.1\\node_modules\\npm\\bin\\npm-cli.js', npm_lifecycle_event: 'precommit', npm_lifecycle_script: 'lint-staged && opt --in pre-commit --verbose --exec "npm start validate"', npm_node_execpath: 'C:\\Program Files\\nodejs\\node.exe', npm_package_author_email: 'kent@doddsfamily.us', npm_package_author_name: 'Kent C. Dodds', npm_package_author_url: 'http://kentcdodds.com/', npm_package_bin_cross_env: 'dist/bin/cross-env.js', npm_package_bin_cross_env_shell: 'dist/bin/cross-env-shell.js', npm_package_bugs_url: 'https://github.com/kentcdodds/cross-env/issues', npm_package_config_commitizen_path: 'node_modules/cz-conventional-changelog', npm_package_dependencies_cross_spawn: '^5.1.0', npm_package_dependencies_is_windows: '^1.0.0', npm_package_dependencies_njstrace: '^0.0.2', npm_package_description: 'Run scripts that set and use environment variables across platforms', npm_package_devDependencies_all_contributors_cli: '^4.0.1', npm_package_devDependencies_babel_cli: '^6.24.1', npm_package_devDependencies_babel_core: '^6.23.1', npm_package_devDependencies_babel_jest: '^20.0.3', npm_package_devDependencies_babel_preset_env: '^1.2.0', npm_package_devDependencies_babel_preset_stage_2: '^6.22.0', npm_package_devDependencies_babel_register: '^6.23.0', npm_package_devDependencies_codecov: '^2.3.0', npm_package_devDependencies_commitizen: '^2.9.6', npm_package_devDependencies_cz_conventional_changelog: '^2.0.0', npm_package_devDependencies_eslint: '^3.17.0', npm_package_devDependencies_eslint_config_kentcdodds: '^12.0.0', npm_package_devDependencies_husky: '^0.14.3', npm_package_devDependencies_jest_cli: '^20.0.4', npm_package_devDependencies_lint_staged: '^3.3.1', npm_package_devDependencies_nps: '^5.0.3', npm_package_devDependencies_nps_utils: '^1.1.2', npm_package_devDependencies_opt_cli: '^1.5.1', npm_package_devDependencies_prettier_eslint_cli: '^4.1.1', npm_package_devDependencies_semantic_release: '^6.3.6', npm_package_devDependencies_validate_commit_msg: '^2.11.1', npm_package_engines_node: '>=4.0', npm_package_eslintConfig_extends_0: 'kentcdodds', npm_package_eslintConfig_extends_1: 'kentcdodds/jest', npm_package_eslintConfig_rules_max_len_0: 'error', npm_package_eslintConfig_rules_max_len_1: '80', npm_package_files_0: 'dist', npm_package_gitHead: 'f374ccf8e24e8b1b9985142e4c068f56454292c7', npm_package_homepage: 'https://github.com/kentcdodds/cross-env#readme', npm_package_jest_coverageThreshold_global_branches: '100', npm_package_jest_coverageThreshold_global_functions: '100', npm_package_jest_coverageThreshold_global_lines: '100', npm_package_jest_coverageThreshold_global_statements: '100', npm_package_jest_testEnvironment: 'node', npm_package_license: 'MIT', npm_package_lint_staged___js_0: 'prettier-eslint --write', npm_package_lint_staged___js_1: 'git add', npm_package_main: 'dist/index.js', npm_package_name: 'cross-env', npm_package_readmeFilename: 'README.md', npm_package_repository_type: 'git', npm_package_repository_url: 'git+https://github.com/kentcdodds/cross-env.git', npm_package_scripts_commitmsg: 'opt --in commit-msg --exec "validate-commit-msg"', npm_package_scripts_precommit: 'lint-staged && opt --in pre-commit --verbose --exec "npm start validate"', npm_package_scripts_start: 'nps', npm_package_scripts_test: 'nps test', npm_package_version: '0.0.0-semantically-released', NUMBER_OF_PROCESSORS: '4', NVM_HOME: 'C:\\nvm', NVM_SYMLINK: 'C:\\Program Files\\nodejs', PATH: 'C:\\nvm\\v8.2.1\\node_modules\\npm\\bin\\node-gyp-bin;E:\\Projects\\repos\\cross-env-copy\\node_modules\\.bin;C:\\Program Files\\Git\\mingw64\\libexec\\git-core;C:\\Program Files\\Git\\mingw64\\bin;C:\\Program Files\\Git\\usr\\bin;C:\\Users\\********\\bin;C:\\tools\\cmder\\bin;C:\\tools\\cmder\\vendor\\conemu-maximus5\\ConEmu\\Scripts;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\tools\\cmder\\bin;C:\\tools\\cmder\\vendor\\conemu-maximus5\\ConEmu\\Scripts;C:\\tools\\cmder\\vendor\\conemu-maximus5;C:\\tools\\cmder\\vendor\\conemu-maximus5\\ConEmu;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0;C:\\Program Files\\OpenVPN\\bin;C:\\nvm;C:\\Program Files\\nodejs;C:\\ProgramData\\chocolatey\\bin;C:\\Program Files\\nodejs;C:\\Program Files (x86)\\Yarn\\bin;C:\\Program Files\\Git\\cmd;C:\\Program Files (x86)\\Windows Kits\\8.1\\Windows Performance Toolkit;C:\\tools\\cmder\\vendor\\git-for-windows\\usr\\bin;C:\\Program Files\\Sublime Text 3;C:\\tools\\go\\bin;E:\\workspace\\golang\\bin;C:\\Program Files\\Git\\usr\\bin;C:\\Program Files\\Git\\usr\\share\\vim\\vim74;C:\\tools\\cmder;E:\\Installs\\go-tools\\bin;C:\\Users\\********\\.windows-build-tools\\python27;C:\\nvm\\v8.2.1\\node_modules\\npm\\bin\\node-gyp-bin;C:\\Users\\********\\AppData\\Roaming\\npm\\node_modules\\windows-build-tools\\node_modules\\.bin;C:\\Users\\********\\AppData\\Roaming\\npm\\node_modules\\.bin;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0;C:\\Program Files\\OpenVPN\\bin;C:\\nvm;C:\\Program Files\\nodejs;C:\\ProgramData\\chocolatey\\bin;C:\\Program Files\\nodejs;C:\\Program Files (x86)\\Yarn\\bin;C:\\Program Files\\Git\\cmd;C:\\Program Files (x86)\\Windows Kits\\8.1\\Windows Performance Toolkit;C:\\Users\\********\\AppData\\Local\\Microsoft\\WindowsApps;C:\\nvm;C:\\Program Files\\nodejs;C:\\Users\\********\\AppData\\Roaming\\npm;C:\\Users\\********\\AppData\\Local\\Yarn\\bin;C:\\tools\\cmder;C:\\Program Files\\Microsoft VS Code\\bin;C:\\tools\\mysql\\current\\bin;C:\\Program Files\\Git\\usr\\bin;C:\\Program Files\\Git\\usr\\share\\vim\\vim74;C:\\tools\\cmder;C:\\Program Files\\nodejs', PATHEXT: '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JSE;.WSF;.WSH;.MSC', PWD: 'E:/Projects/repos/cross-env-copy', TERM: 'cygwin', _: 'C:/Program Files/nodejs/node.exe' } ``` **NOTE**: I have stripped possibly irrelevant env variables for the sake of brevity.


This PR adds a filter and reduce step to the process.env values. I was able to test this with cross-env successfully. I have also added myself to contributors.

sudo-suhas commented 7 years ago

I can do the changes required for passing the build on node v0.10. However, I see that you are planning to drop support for node < 4 in #211.

ta2edchimp commented 7 years ago

Hi there,

let's just drop node versions < 4, as they all have reached their End-of-Life.
I will also merge the updated PR #211 later today and we should be good then.

There's only one thing I'm uncomfortable with: I would prefer to strictly filter the undefined properties of process.env:

process.env[ key ] !== undefined
sudo-suhas commented 7 years ago

I would prefer to strictly filter the undefined properties of process.env

Lol.. I tried this first and got eslint error - [eslint] Unexpected use of undefined. (no-undefined). Tried void 0 but that complained about magic numbers πŸ˜†. So I gave up and went with null.

Edit: Rebased with requested changes.

codecov-io commented 7 years ago

Codecov Report

Merging #213 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #213   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines          86     88    +2     
=====================================
+ Hits           86     88    +2
Impacted Files Coverage Ξ”
bin/index.js 100% <100%> (ΓΈ) :arrow_up:

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 05f5804...15fa7d4. Read the comment docs.

ta2edchimp commented 7 years ago

Well, that's strange ... seems to work now, according to Travis CI.
I'll have a more thorough look later today and most probably merge, then.

Thanks a lot for the work! πŸ‘

ta2edchimp commented 7 years ago

I fetched your PR branch, rebased and merged.
A new version (1.5.2) has also been released already.

Closing this PR as it has not been closed automatically on merge.