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
- 10.2K All Categories
- 36 LFX Mentorship
- 89 LFX Mentorship: Linux Kernel
- 506 Linux Foundation Boot Camps
- 279 Cloud Engineer Boot Camp
- 104 Advanced Cloud Engineer Boot Camp
- 49 DevOps Engineer Boot Camp
- 41 Cloud Native Developer Boot Camp
- 2 Express Training Courses
- 2 Express Courses - Discussion Forum
- 1.8K Training Courses
- 17 LFC110 Class Forum
- 5 LFC131 Class Forum
- 20 LFD102 Class Forum
- 148 LFD103 Class Forum
- 13 LFD121 Class Forum
- 61 LFD201 Class Forum
- LFD210 Class Forum
- 1 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum
- 23 LFD254 Class Forum
- 570 LFD259 Class Forum
- 100 LFD272 Class Forum
- 1 LFD272-JP クラス フォーラム
- 1 LFS145 Class Forum
- 23 LFS200 Class Forum
- 739 LFS201 Class Forum
- 1 LFS201-JP クラス フォーラム
- 1 LFS203 Class Forum
- 46 LFS207 Class Forum
- 298 LFS211 Class Forum
- 53 LFS216 Class Forum
- 46 LFS241 Class Forum
- 41 LFS242 Class Forum
- 37 LFS243 Class Forum
- 10 LFS244 Class Forum
- 27 LFS250 Class Forum
- 1 LFS250-JP クラス フォーラム
- 131 LFS253 Class Forum
- 998 LFS258 Class Forum
- 10 LFS258-JP クラス フォーラム
- 87 LFS260 Class Forum
- 126 LFS261 Class Forum
- 31 LFS262 Class Forum
- 79 LFS263 Class Forum
- 15 LFS264 Class Forum
- 10 LFS266 Class Forum
- 17 LFS267 Class Forum
- 17 LFS268 Class Forum
- 21 LFS269 Class Forum
- 201 LFS272 Class Forum
- 1 LFS272-JP クラス フォーラム
- 213 LFW211 Class Forum
- 154 LFW212 Class Forum
- 899 Hardware
- 217 Drivers
- 74 I/O Devices
- 44 Monitors
- 115 Multimedia
- 208 Networking
- 101 Printers & Scanners
- 85 Storage
- 749 Linux Distributions
- 88 Debian
- 64 Fedora
- 14 Linux Mint
- 13 Mageia
- 24 openSUSE
- 133 Red Hat Enterprise
- 33 Slackware
- 13 SUSE Enterprise
- 355 Ubuntu
- 473 Linux System Administration
- 38 Cloud Computing
- 69 Command Line/Scripting
- Github systems admin projects
- 94 Linux Security
- 77 Network Management
- 108 System Management
- 49 Web Management
- 63 Mobile Computing
- 22 Android
- 27 Development
- 1.2K New to Linux
- 1.1K Getting Started with Linux
- 528 Off Topic
- 127 Introductions
- 213 Small Talk
- 20 Study Material
- 796 Programming and Development
- 264 Kernel Development
- 498 Software Development
- 923 Software
- 258 Applications
- 182 Command Line
- 2 Compiling/Installing
- 76 Games
- 316 Installation
- 54 All In Program
- 54 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)