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

Added in comments in .nvmrc #2288

Closed
wants to merge 10 commits into from
Closed

Conversation

Yash-Singh1
Copy link
Contributor

@Yash-Singh1 Yash-Singh1 commented Aug 20, 2020

Overview

I added in support for comments in the .nvmrc file.

List of Updates

  • Modified the nvm_rc_version to strip comments
  • Removed support for spaces and #'s inside nvm aliases

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's please separate out the changes that are related to "nvmrc comments", and the rest.

@@ -446,6 +454,28 @@ Found '/path/to/project/.nvmrc' with version <5.9>
Now using node v5.9.1 (npm v3.7.3)
```

`.nvmrc` files may have comments. The following is the notation to have a comment inside a `.nvmrc` file:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty major feature that I'm pretty convinced I do not want. In the future it might be best to discuss this sort of thing via issue first, so you don't end up doing wasted work.

README.md Outdated Show resolved Hide resolved
@ljharb
Copy link
Member

ljharb commented Aug 20, 2020

I think before deciding what code changes are needed, to this PR or nvm itself, we should have nonzero discussion about what the goal is, and why this feature is needed.

@ljharb ljharb added the feature requests I want a new feature in nvm! label Aug 20, 2020
@ljharb

This comment has been minimized.

nvm.sh Outdated
Comment on lines 349 to 354
NVM_RC_VERSION="$(command head -n 1 "${NVMRC_PATH}" | command tr -d '\r')" || command printf ''
NVM_RC_VERSION="$(command cat "${NVMRC_PATH}" | command tr -d '\r' | sed -e 's/\s*#.*$//' | sed -e '/^\s*$/d' | command tr -d '\n' | command tr -d '\t' | command head -n 1 | sed -e 's/[[:space:]]//g')" || command printf ''
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recently, I had a .nvmrc file inside one of my projects. As you know, most projects have only one, and I wanted to add in some comments. These line changes above, are the main part of this pull request. Instead of running head on the file, I replaced it for cat. Next, the next two sed commands remove comments. I also removed newlines and tabs inside. After that, I ran head -n 1 on everything to get only the first line. In the case of the following scenario:

node # This is the nvm version

There is going to be a space thereafter where it says node. Next, I also removed spaces for that scenario. That is a quick read of what is happening in the main part of the pull request.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, but why did you want to add comments? The file is named "nvm rc", who needs the extra hint that it's for nvm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your point. The comment does not have to be:

node # This is for nvm

It can also be:

v14.0.23 # This is the node version to be installed

As you can see, the comment gives some description to someone who didn’t come across .nvmrc. I wanted to add comments to give a quick explanation of the .nvmrc. The answer is the same as asking, why do you ever write comments. To make sure that anyone else understands your code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right - but comments in code should never explain "what", only "why" or history/motivation, and in this case, git log .nvmrc should tell you precisely that in the commit messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't hurt to add in support for comments. It gives people the choice to describe why the .nvmrc is there and what is in it. I still couldn't understand why you don't want comments in .nvmrc.

Copy link
Contributor Author

@Yash-Singh1 Yash-Singh1 Aug 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb

Say we are making a change that also allows the npm version. Then, if the .nvmrc allows comments, it will look like this:

# This is a comment
node # This is the nodejs version
v6.14.7 # This is the npm version

In that scenario, if we remove the head -n 1 at the end, the output will be:

node
v6.14.7

In the code, we can simply pick whether we want the first version or the second version (The node version or the npm version). I don't see how it complicates everything.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb those are good puritan points, and perhaps you're right, but I think in our case on a large project, where people don't even necessarily know what nvm is let alone an nvmrc it can be helpful to explain e.g. "# we're fixing to 16.2.0 because of this bug " and or simply re-document the default behaviours "this version will be used by default when you run nvm install. It's an agreed upon, tested & supported node version for the project." Coordinating communications on projects with people with a wide range of backgrounds/seniority can mean that documenting "what" is helpful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t that what git is for?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I don't think expecting my interns to read a git lens comment is as easy as having them read the file contents.

Should we just put all our efforts into teaching interns the correct methodologies and stop perpetuating incompetence in our industry? Probably, yes.

On the other hand, if the world is covered in unnecessary thorns, is it easier to wear shoes than of rid the world of thorns? Sometimes the pragmatic option is the correct option.

Anyway - you make good points, and I can appreciate why you'd say no. JSON doesn't have comments either.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb I can maybe put good example - in comment there could be link to issue that require to keep node in specific version due to some complications something like - for now we have this comment on CI workflows with node version - i would like to move it into nvmrc without loosing information why it is locked to particular version.

v16.10.0 # https://github.com/facebook/jest/issues/11956

But as keeping nvm simple is also good valid point.

@sladyn98
Copy link
Contributor

@ljharb Do we want to take action on this one with the intention of getting it merged ?

@ljharb
Copy link
Member

ljharb commented Apr 16, 2021

@sladyn98 I'm still not convinced it's worth the complexity.

@sladyn98
Copy link
Contributor

@ljharb if you plan to not merge in the future don't you think we should close it

@ljharb
Copy link
Member

ljharb commented Apr 19, 2021

I'm undecided, which is why it's left open. I don't believe in closing a PR unless it's certain to never land.

@Yash-Singh1 Yash-Singh1 changed the title Added in comments in .nvmrc and more Added in comments in .nvmrc Apr 19, 2021
@penx
Copy link

penx commented Apr 28, 2021

Would be very useful for "make sure you update the <docker container/CI config/package.json engines> too!" style comments

@Yash-Singh1
Copy link
Contributor Author

Do you mean allowing a .nvmrc, like this?:

“some comment”
node

The reason that this shouldn’t be added is because it removes support for aliases with double quotes and anyways, the following works:

# some comment
node

@penx
Copy link

penx commented Apr 28, 2021

No, I mean the feature (as you propose it) would be useful for adding comments to the .nvmrc to remind people to change values elsewhere.

e.g.

# this should be kept in sync with the <docker container/CI config/package.json engines/readme/wiki>
14

This was partially in response to

why this feature is needed

further up.

@ljharb
Copy link
Member

ljharb commented Apr 28, 2021

@penx isn't a CI job that fails when those aren't in sync much more effective than a comment?

@penx
Copy link

penx commented Apr 28, 2021

@ljharb yes it would, but it may not always be feasible or convenient

e.g.

  • the place to keep in sync is not within the repository, such as a wiki or external repository
  • the format to keep in sync is hard to parse e.g. numbers contained in paragraphs in a markdown readme where there may be multiple mentions and the wording could change ("prerequisites: node >= 12 and <= 14.*, npm 7")
  • the contents of .nvmrc doesn't exactly match what you're trying to sync with (e.g. semver, ranges)
  • the management of the CI pipelines are done by a different team/company and it is not trivial to add new ones or modify existing ones

@jasonkarns
Copy link

Another useful example of a useful comment (and one that isn't particularly suited for git commit messages, IMO):

14.0.0

# symlinked as .nvmrc/.node-version to support nvm, nodenv and asdf with a single file
# until such time as https://github.com/nodejs/version-management/issues/13 is resolved

@ljharb
Copy link
Member

ljharb commented Feb 7, 2022

@jasonkarns i mean, the symlink part is already self-evident from "the actual symlinks in the filesystem"; it's just the issue URL that has no place to go. but fair, that's a good use case.

@jasonkarns
Copy link

@ljharb if we assume that it being a symlink is even evident at all.

Some ides/editors don't distinguish that a file is a symlink; and even those that do, don't necessarily make it -obvious-.

Add insult to injury, these are hidden files so many project views wouldn't show them in a listing.

All that to say: I -had- this exact comment in a symlink in a project recently and had to remove it for compatibility. :)

@JohnAlbin
Copy link

JohnAlbin commented May 2, 2022

First, about why you want comments in the .nvmrc file:

git log is horrible for file type discovery. git log .nvmrc is going to show "Initial commit" for 100% of my projects.

If you are explaining what an .nvmrc file is for, wouldn't that be better put in a docs/DEVELOPMENT.md file? Your project readme files should explain what tools and commands are needed for development and how they are used. Having new developers poke around in all the files and to try to figure out what is important and why is not a great onboarding strategy. However, having an intern make notes about project files (even if the notes are "???") and then tasking them with expanding a project's developer docs is a GREAT intern task, @danielrob.

Several people have pointed this out above, so I'd like to reiterate the main reason I have comments in configuration files is to document bugs that force me to use a specific configuration option, .e.g. "Bug X causes Y so we set our config to Z as a work-around. [link to issue]" That kind of information needs to be right next to the configuration or someone (maybe even me!) will "fix it" later not realizing it causes a bug, e.g. "Huh, this file is out-of-date. The value of v14.13.1 should be updated to the latest v14.19.1. Oops. I broke something."

IMO, I think all configuration files should be able to have comments in them. Don't get me started on .json files for configuration.

Lastly, I was looking at the code this PR changes and noticed this line in the original nvm.sh file:

NVM_RC_VERSION="$(command head -n 1 "${NVMRC_PATH}" | command tr -d '\r')" || command printf ''

Only the first line in .nvmrc is ever used to determine the value of NVM_RC_VERSION. That means all the other lines in .nvmrc can be used for comments without any code changes. I tested this by creating a .nvmrc file with an intentionally blank first line:


v14

And I got an error: Warning: empty .nvmrc file found

And when I changed the file to:

v14
# This is a comment
// And so is this
; And so is this
And so is this

The nvm use command reported Found '.nvmrc' with version <14> and ignored all other lines except the first.

So, all we really need to do is make this officially supported in docs; no code changes, afaics.

@ljharb
Copy link
Member

ljharb commented May 2, 2022

@JohnAlbin however, if you do that, it's guaranteed to break in the future, because eventually nvm will support parsing additional content from successive lines.

@JohnAlbin
Copy link

@ljharb said:

if you do that, it's guaranteed to break in the future, because eventually nvm will support parsing additional content from successive lines.

Agreed.

From my previous comment:

…all we really need to do is make this officially supported in docs… (emphasis added)

Which is why I said we need to decide how to document how comments should work.

Right now, afaics, the entire .nvmrc syntax specification is:

To add the official ability to comment the file we need to decide what format comments are allowed to use. And whether we want to reserve the second or third lines for forward compatibility with future features.

FYI, I prefer the # comment syntax as it matches most of the other dot file comment syntaxes.

@r-moore
Copy link

r-moore commented Jul 26, 2022

Several people have pointed this out above, so I'd like to reiterate the main reason I have comments in configuration files is to document bugs that force me to use a specific configuration option, .e.g. "Bug X causes Y so we set our config to Z as a work-around. [link to issue]" That kind of information needs to be right next to the configuration or someone (maybe even me!) will "fix it" later not realizing it causes a bug, e.g. "Huh, this file is out-of-date. The value of v14.13.1 should be updated to the latest v14.19.1. Oops. I broke something."

This is our exact use case

We pin a project to a specific node version and I would like to leave a comment in nvmrc to say why we are using that version (e.g. "don't upgrade to v16 yet because X breaks")

Yes we could put that comment somewhere else in documentation, but this seems like the best place to leave a reminder (so that the person bumping the version will see it)

@JohnAlbin
Copy link

I created a new issue so we can discuss adding comments to .nvmrc without mixing that discussion up with our opinions about the code in this PR.

#3336 Allow comments in .nvmrc config file

@ljharb ljharb closed this in 29dce5e Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature requests I want a new feature in nvm!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants