Skip to content

Fix nodejs: Create directory before executing script #183

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

Merged
merged 2 commits into from
Mar 19, 2024

Conversation

TheZoker
Copy link
Contributor

@TheZoker TheZoker commented Mar 6, 2024

Something I did not notice during my testing was, that the directory for nvm needs to be created first before the script can be installed. This PR fixes this.

@TheZoker
Copy link
Contributor Author

TheZoker commented Mar 6, 2024

@mafredri @matifali Can you guys please have a look at this? Thanks!

Edit:
So this is the issue I see in my logs:

Failed to install nvm: You have $NVM_DIR set to "$HOME/.nvm/nvm", but that directory does not exist. Check your profile files and environment.

But the directory does exists.

When I set this in the module config:

module "nodejs" {
  source               = "registry.coder.com/modules/nodejs/coder"
  version              = "1.0.8"
  agent_id             = coder_agent.main.id
  nvm_install_prefix   = "/home/${local.username}/.nvm"
  node_versions        = [
    "18",
    "20"
  ]
  default_node_version = "20"
}

it works.

But this seems to not work by default:

module "nodejs" {
  source               = "registry.coder.com/modules/nodejs/coder"
  version              = "1.0.8"
  agent_id             = coder_agent.main.id
  nvm_install_prefix   = "$HOME/.nvm"
  node_versions        = [
    "18",
    "20"
  ]
  default_node_version = "20"
}

Any idea why this is the case and how to fix it?

@matifali matifali requested a review from mafredri March 7, 2024 10:26
@mafredri
Copy link
Member

mafredri commented Mar 7, 2024

Ah right, it makes sense that the latter doesn't work because we're trying to install it into the literal $HOME directory (echo \$HOME), not whatever $HOME expands to.

I think one question we need to ask is, should our modules support variables names in inputs? If so, we need to handle that consistently across our modules.

One approach is to allow "well known" variables/syntax, like $HOME, ${HOME} and ~. Then we can string-replace those, where applicable.

Allowing variable expansion is always a bit iffy, IMO, as it could be possible to inject things or make the script behave in unexpected ways. Just imagine if someone gave this as an input: $(rm -rf /).

Next week when @matifali is back, I'd love to have his input on this as well.

@matifali
Copy link
Member

I think its a sane choice to not allow variable names as directories.

@TheZoker
Copy link
Contributor Author

What's the best way to achieve this?

@matifali
Copy link
Member

@TheZoker We can treat the input as a relative directory to $HOME and expand it within the script. This will endure that user can not pass something dangerous.

For example,

module "nodejs" {
  source               = "registry.coder.com/modules/nodejs/coder"
  version              = "1.0.8"
  agent_id             = coder_agent.main.id
  nvm_install_prefix   = ".nvm". # relative to $HOME
  node_versions        = [
    "18",
    "20"
  ]
  default_node_version = "20"
}

and then within the coder_script we do,

export NVM_DIR="$HOME/$${INSTALL_PREFIX}/nvm"
mkdir -p "$NVM_DIR"

What do you think @mafredri?

@mafredri
Copy link
Member

mafredri commented Mar 18, 2024

What do you think @mafredri?

@matifali Seems like an OK solution 👍🏻.

Explicitly noting that we have to set $HOME/ in NVM_DIR (as you demonstrated) because otherwise the relative directory might be affected by coder_agent.dir.

@TheZoker
Copy link
Contributor Author

@mafredri @matifali Can you guys please have a look at the latest commit?

@matifali matifali merged commit ee80d1f into coder:main Mar 19, 2024
@TheZoker TheZoker deleted the fix-nvm branch March 19, 2024 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants