Skip to content

fix(downloads): strip shell comments when copying download snippets - #9352

Closed
rxliuli wants to merge 1 commit into
nodejs:mainfrom
rxliuli:fix/strip-comments-on-copy
Closed

rxliuli wants to merge 1 commit into
nodejs:mainfrom
rxliuli:fix/strip-comments-on-copy

Conversation

@rxliuli

@rxliuli rxliuli commented Nov 28, 2026

Copy link
Copy Markdown

Description

Since macOS Catalina, the default interactive shell on macOS is zsh, where INTERACTIVE_COMMENTS is disabled by default. Clicking Copy to clipboard on any download snippet at https://nodejs.org/en/download and pasting it into Terminal.app emits zsh: command not found: # for every comment line and passes inline # Should print "..." comments as literal CLI arguments.

This PR keeps the explanatory # comments visible in the rendered code box for readability, while stripping shell # comments from the clipboard text when copying download snippets:

  • Adds an optional copyText?: string prop to BaseCodeBox (and forwards it through CodeBox), preserving existing containerRef.current?.textContent behavior for all other callers.
  • Adds stripShellComments() in apps/site/util/download/snippet.ts to remove full-line # comments and unquoted trailing inline # comments while preserving # inside single/double/backtick quotes and parameter expansions (e.g. ${VAR#prefix}), and collapsing redundant blank lines.
  • Updates ReleaseCodeBox to pass copyText={stripShellComments(parsedContent)} to CodeBox.
  • Adds unit tests for both stripShellComments and BaseCodeBox's copyText prop, plus a changeset for @node-core/ui-components.

Validation

  • Added unit tests in apps/site/util/__tests__/snippet.test.mjs and packages/ui-components/src/Common/BaseCodeBox/__tests__/index.test.jsx.
  • Ran pnpm format, pnpm test, pnpm lint, pnpm lint:types, and pnpm build — all passed with zero errors.

Related Issues

Refs #9350

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run pnpm format to ensure the code follows the style guide.
  • I have run pnpm test to check if all tests are passing.
  • I have run pnpm build to check if the website builds without errors.
  • I've covered new added functionality with unit tests if necessary.

@rxliuli
rxliuli requested a review from a team as a code owner November 28, 2026 11:51
@vercel

vercel Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nodejs-org Ready Ready Preview Nov 28, 2026 11:52am UTC

Request Review

@avivkeller avivkeller left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think copying the comments is important, the copy button is meant to copy the entire snippet contents

@rxliuli

rxliuli commented Nov 29, 2026

Copy link
Copy Markdown
Author

The reason for stripping # comments on copy is a concrete macOS breakage: macOS's default interactive shell (/bin/zsh) ships with INTERACTIVE_COMMENTS disabled by default.

When any macOS user clicks "Copy to clipboard" on nodejs.org/en/download and pastes into Terminal, every # ... comment line (and every inline # Should print "..." comment) is executed as a command, producing errors on all 7 macOS snippets — e.g.:

zsh: command not found: #
zsh: no matches found: (add to your profile to persist):

(Full clean-VM screenshots are in #9350.)

If the copy button must strictly copy everything displayed inside the code block, an alternative way to solve this without hiding anything from the copy action would be to move explanatory text out of the code block (or split commands into separate copyable blocks without # comments), or remove # comments from the snippets themselves. Would you prefer one of those approaches instead?

@ovflowd

ovflowd commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

When any macOS user clicks "Copy to clipboard" on nodejs.org/en/download and pastes into Terminal, every # ... comment line (and every inline # Should print "..." comment) is executed as a command, producing errors on all 7 macOS snippets — e.g.:

Users are not meant to copy the whole snippet and one shot it. The user is supposed to copy line by line and wait the output of each command, otherwise if they one shot they cannot troubleshoot issues.

Moreover, in this sense the snippets are partially illustrative.

@ovflowd ovflowd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is over-engineering of removing comments, and the current behavior is intentional.

@rxliuli rxliuli closed this Nov 29, 2026

This branch was successfully deployed

1 active deployment
Preview — 6fae26f8 Deployed Nov 28, 2026 by vercel[bot]
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