Troubles with ch-15 \ lab 1
I feel like I understand the ask, but I've honestly tried multiple runs (I'm on windows 10 BTW).
For the spawnSync, I've tried, what I feel like are, all variations where I assign options to be:
{ env: { MY_ENV_VAR: myEnvVar }}
However, every time it runs, I get every process environment variable; including the value supplied from test.ts and injected into the index method argument of myEnvVar.
I honestly don't know if this is a windows environment issue with Node, or a bad test (not accounting for how it works with windows), or I am doing it wrong. Who knows, it could even be all of them
Please, what am I doing wrong here?
Thanks!
P.s. I tried to be short on the answer.
Comments
-
I just tried this out on a mac and I found out it passes. This lab should be re-evaluated for windows environments.
0 -
Hey @kellv thanks for feeding back. All of the labs were written to work on windows, but this one is especially tricky to validate as OS's have a habit of injecting env vars into child processes. I need more info about what's going wrong, but I suspect there's an env var being injected unexpectedly by Windows - this could even be happening based on a particular way your OS is configured.
are you using cmd or powershell?
would you mind modifying the child.js so that on line 2 (just under 'use strict') you have:
return console.log(process.env)
and then run the following command:
node -e "child_process.spawn(process.execPath, ['child.js'], {stdio: 'inherit', env: {}})"
and paste the output here?
0 -
Hi David,
Thanks for responding here.
The local setup on window is powershell using nvs use 12
When doing the argument injection I originally mentioned above, it is appending the property value to the already existing env vars object, which in itself is rather interesting.My findings were from altering the file by adding a console log for env similarly like you mentioned above as I was trying to debug my frustrations
I'll go back through and do the following above with your node -e execution later. (I'm back into studying mode again).
0 -
@davidmarkclements I just ran your command in powershell and it I get:
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + undefined - 'is set' at Object.<anonymous> (C:\Users\kelly\jsnad\Labs-2020-06-07\ch-15\labs-1\child.js:9:8) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: 'ERR_ASSERTION', actual: undefined, expected: 'is set', operator: 'strictEqual' }
So. it looks like that works as expected
0 -
hey @kellv can you make sure to modify child.js so line two is exactly:
return console.log(process.env)
and then run the command
I want to exit early (return) and log out the env
0 -
As you officially requested sir. (and again, thank you)
{ HOMEDRIVE: 'C:', HOMEPATH: '\\Users\\kelly', LOGONSERVER: '\\\\DESKTOP-XXXXXX', PATH: 'C:\\Users\\kelly\\AppData\\Local\\nvs\\node\\12.18.0\\x64;C:\\Python38\\Scripts\\;C:\\Python38\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\ \System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\ProgramData\\chocolatey\\bin;C:\\Program Files\\dotnet\\;C:\\Program Files\\Git\\cmd;C:\\Users\\kelly\\App Data\\Local\\Microsoft\\WindowsApps;C:\\Users\\kelly\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Users\\kelly\\AppData\\Local\\JetBrains;C:\\Users\\kelly\\AppData\\Local\ \nvs\\', SYSTEMDRIVE: 'C:', SYSTEMROOT: 'C:\\WINDOWS', TEMP: 'C:\\Users\\kelly\\AppData\\Local\\Temp', USERDOMAIN: 'DESKTOP-XXXXXX', USERNAME: 'kelly', USERPROFILE: 'C:\\Users\\kelly', WINDIR: 'C:\\WINDOWS' }
And to add, I get the same results running in powershell and cmd
0 -
Hey, got the same thing. Works on mac , not on windows. Error received: "child process should only have one env var".
1 -
hey @kellv to get that output did you run
node -e "child_process.spawn(process.execPath, ['child.js'], {stdio: 'inherit', env: {}})"
or
node child.js
0 -
Hello @davidmarkclements,
I'm struggling to get it on Windows too, BTW I edited
child.js
'use strict' const assert = require('assert') const clean = (env) => Object.fromEntries( Object.entries(env).filter(([k]) => !/^(_.*|pwd|shlvl)/i.test(k)) ) const env = clean(process.env) console.log(env) assert.strictEqual(env.MY_ENV_VAR, 'is set') assert.strictEqual( Object.keys(env).length, 1, 'child process should have only one env var' ) console.log('passed!')
Adding the
console.log(env)
tried running it via the commandMINGW64 ~/Desktop/applications/certification/labs/ch-15/labs-1 $ node -e "child_process.spawn(process.execPath, ['child.js'], {stdio: 'inherit', env: { MY_ENV_VAR: 'is set' }})" { HOMEDRIVE: 'C:', HOMEPATH: '\\Users\\elaconico', LOGONSERVER: '...', MY_ENV_VAR: 'is set', PATH: 'C:\\Program Files\\Git\\mingw64\\bin;C:\\Program Files\\Git\\usr\\bin;C:\\Users\\elaconico\\bin;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Python27\\;C:\\Python27\\Scripts;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\Program Files\\Docker\\Docker\\Resources\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Microsoft VS Code\\bin;C:\\Program Files\\PuTTY\\;C:\\xampp\\php;C:\\composer;C:\\Program Files\\TortoiseSVN\\bin;C:\\Program Files\\Redis\\;C:\\Users\\elaconico\\AppData\\Roaming\\nvm;C:\\nodejs;C:\\Program Files (x86)\\GnuWin32\\bin;C:\\Program Files\\Git\\cmd;C:\\Program Files\\heroku\\bin;C:\\Ruby26-x64\\bin;C:\\Users\\elaconico\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\OpenVPN\\bin;C:\\Users\\elaconico\\AppData\\Roaming\\Composer\\vendor\\bin;C:\\Program Files\\MongoDB\\Server\\4.2\\bin;C:\\ffmpeg\\bin;C:\\Program Files\\WhoLockMe', SYSTEMDRIVE: 'C:', SYSTEMROOT: 'C:\\WINDOWS', TEMP: 'C:\\Users\\ELACON~1\\AppData\\Local\\Temp', USERDOMAIN: '...', USERNAME: 'elaconico', USERPROFILE: 'C:\\Users\\elaconico', WINDIR: 'C:\\WINDOWS' } assert.js:118 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: child process should have only one env var at Object.<anonymous> (C:\Users\elaconico\Desktop\applications\certification\labs\ch-15\labs-1\child.js:11:8) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) at internal/main/run_main_module.js:17:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: 12, expected: 1, operator: 'strictEqual' }
blurred some of the value with
...
and yes, it seems it is injecting unwanted env vars, should we modify this then?Object.entries(env).filter(([k]) => !/^(_.*|pwd|shlvl)/i.test(k))
Thank you for your support!
0 -
UPDATE
It seems to be a regex issue,
I tried modifying theclean
functionFROM
const clean = (env) => Object.fromEntries( Object.entries(env).filter(([k]) => !/^(_.*|pwd|shlvl)/i.test(k)) )
TO
const clean = (env) => Object.fromEntries( Object.entries(env).filter(([k]) => /^(MY_ENV_VAR)/i.test(k)) )
OUTPUT
MINGW64 ~/Desktop/applications/certification/labs/ch-15/labs-1 $ node -e "child_process.spawn(process.execPath, ['child.js'], {stdio: 'inherit', env: { MY_ENV_VAR: 'is set' }})" { MY_ENV_VAR: 'is set' } passed!
tried intentionally to make it not work
const clean = (env) => Object.fromEntries( Object.entries(env).filter(([k]) => /^(MY_ERROR_VAR)/i.test(k)) )
OUTPUT
MINGW64 ~/Desktop/applications/certification/labs/ch-15/labs-1 $ node -e "child_process.spawn(process.execPath, ['child.js'], {stdio: 'inherit', env: { MY_ENV_VAR: 'is set' }})" {} assert.js:118 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + undefined - 'is set' at Object.<anonymous> (C:\Users\elaconico\Desktop\applications\certification\labs\ch-15\labs-1\child.js:10:8) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: 'ERR_ASSERTION', actual: undefined, expected: 'is set', operator: 'strictEqual' }
Hope this helps.
0 -
I had the same issue, switched to Ubuntu WSL to pass
0 -
@elaconico - unfortunately your changes just lead to asserting that true is true, we don't just want MY_ENV_VAR to be set, we want it to to be the ONLY env var that is set
if you could console log
env
in the clean function and post results here I can take a look at seeing what else is in the way here0 -
I almost managed to ovewrite the environment variables but on the
assertion
i get another one variable, apart from the one that we need.I create
an options object
, where i add theenv variable
that we need:excersice(myEnvVar){ const exec_options = { cwd: null, env: { MY_ENV_VAR: myEnvVar }, encoding: 'utf8', timeout: 0, maxBuffer: 200 * 1024 } const childprocess = spawn(process.execPath, ['-p', 'process.env'], exec_options) return childprocess }
But the output that i get on the
node test
has one more env variable. Can someone give me a clue why this__CF_USER_TEXT_ENCODING:
is added on its own ?0 -
all good now, i added the
{stdio: inherit}
on the exec_options, and added thechild.js
, and all worked.const exec_options = { env: { MY_ENV_VAR: myEnvVar }, stdio: 'inherit' } const childprocess = spawn(process.execPath, ['child.js'], exec_options)
0 -
I run all labs code based on Docker without issues and my host machine is Windows too.
0 -
@krave that makes sense because Docker is generally linux, so really you're running the code on linux.
0 -
Aha, really love to write code based on docker now. Thanks to WSL2 and VScode remote extensions.
0 -
nice
0 -
Since child process inherits parent process.env by default, I just did
process.env = { ... }
beforespawn()
.0 -
@doruk what we're trying to demonstrate here is that the env option passed to spawn creates a clean env. On Windows, that's not entirely true (but it mostly is). So while your approach will cause you to pass the item, it's obviously not a good idea to clear all of process env in the parent process in real-world scenarios.
0
Categories
- All Categories
- 217 LFX Mentorship
- 217 LFX Mentorship: Linux Kernel
- 791 Linux Foundation IT Professional Programs
- 353 Cloud Engineer IT Professional Program
- 178 Advanced Cloud Engineer IT Professional Program
- 82 DevOps Engineer IT Professional Program
- 147 Cloud Native Developer IT Professional Program
- 137 Express Training Courses
- 137 Express Courses - Discussion Forum
- 6.2K Training Courses
- 47 LFC110 Class Forum - Discontinued
- 71 LFC131 Class Forum
- 42 LFD102 Class Forum
- 226 LFD103 Class Forum
- 18 LFD110 Class Forum
- 38 LFD121 Class Forum
- 18 LFD133 Class Forum
- 7 LFD134 Class Forum
- 18 LFD137 Class Forum
- 71 LFD201 Class Forum
- 4 LFD210 Class Forum
- 5 LFD210-CN Class Forum
- 2 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum - Discontinued
- 2 LFD233 Class Forum
- 4 LFD237 Class Forum
- 24 LFD254 Class Forum
- 697 LFD259 Class Forum
- 111 LFD272 Class Forum
- 4 LFD272-JP クラス フォーラム
- 12 LFD273 Class Forum
- 148 LFS101 Class Forum
- 1 LFS111 Class Forum
- 3 LFS112 Class Forum
- 2 LFS116 Class Forum
- 4 LFS118 Class Forum
- LFS120 Class Forum
- 7 LFS142 Class Forum
- 5 LFS144 Class Forum
- 4 LFS145 Class Forum
- 2 LFS146 Class Forum
- 3 LFS147 Class Forum
- 1 LFS148 Class Forum
- 15 LFS151 Class Forum
- 2 LFS157 Class Forum
- 28 LFS158 Class Forum
- 7 LFS162 Class Forum
- 2 LFS166 Class Forum
- 4 LFS167 Class Forum
- 3 LFS170 Class Forum
- 2 LFS171 Class Forum
- 3 LFS178 Class Forum
- 3 LFS180 Class Forum
- 2 LFS182 Class Forum
- 5 LFS183 Class Forum
- 31 LFS200 Class Forum
- 737 LFS201 Class Forum - Discontinued
- 3 LFS201-JP クラス フォーラム
- 18 LFS203 Class Forum
- 134 LFS207 Class Forum
- 2 LFS207-DE-Klassenforum
- 1 LFS207-JP クラス フォーラム
- 302 LFS211 Class Forum
- 56 LFS216 Class Forum
- 52 LFS241 Class Forum
- 48 LFS242 Class Forum
- 38 LFS243 Class Forum
- 15 LFS244 Class Forum
- 2 LFS245 Class Forum
- LFS246 Class Forum
- 48 LFS250 Class Forum
- 2 LFS250-JP クラス フォーラム
- 1 LFS251 Class Forum
- 152 LFS253 Class Forum
- 1 LFS254 Class Forum
- 1 LFS255 Class Forum
- 7 LFS256 Class Forum
- 1 LFS257 Class Forum
- 1.2K LFS258 Class Forum
- 10 LFS258-JP クラス フォーラム
- 118 LFS260 Class Forum
- 159 LFS261 Class Forum
- 42 LFS262 Class Forum
- 82 LFS263 Class Forum - Discontinued
- 15 LFS264 Class Forum - Discontinued
- 11 LFS266 Class Forum - Discontinued
- 24 LFS267 Class Forum
- 22 LFS268 Class Forum
- 30 LFS269 Class Forum
- LFS270 Class Forum
- 202 LFS272 Class Forum
- 2 LFS272-JP クラス フォーラム
- 1 LFS274 Class Forum
- 4 LFS281 Class Forum
- 9 LFW111 Class Forum
- 259 LFW211 Class Forum
- 181 LFW212 Class Forum
- 13 SKF100 Class Forum
- 1 SKF200 Class Forum
- 1 SKF201 Class Forum
- 795 Hardware
- 199 Drivers
- 68 I/O Devices
- 37 Monitors
- 102 Multimedia
- 174 Networking
- 91 Printers & Scanners
- 85 Storage
- 758 Linux Distributions
- 82 Debian
- 67 Fedora
- 17 Linux Mint
- 13 Mageia
- 23 openSUSE
- 148 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 353 Ubuntu
- 468 Linux System Administration
- 39 Cloud Computing
- 71 Command Line/Scripting
- Github systems admin projects
- 93 Linux Security
- 78 Network Management
- 102 System Management
- 47 Web Management
- 63 Mobile Computing
- 18 Android
- 33 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 371 Off Topic
- 114 Introductions
- 174 Small Talk
- 22 Study Material
- 805 Programming and Development
- 303 Kernel Development
- 484 Software Development
- 1.8K Software
- 261 Applications
- 183 Command Line
- 3 Compiling/Installing
- 987 Games
- 317 Installation
- 97 All In Program
- 97 All In Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)