Skip to content

Add VS Code create new project command#22103

Merged
Veykril merged 4 commits into
rust-lang:masterfrom
joshka:joshka/new-project-command
Jun 15, 2026
Merged

Add VS Code create new project command#22103
Veykril merged 4 commits into
rust-lang:masterfrom
joshka:joshka/new-project-command

Conversation

@joshka

@joshka joshka commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #19338.

Summary

Add a VS Code command for creating a new Rust project from the editor.

The command prompts for a project kind, parent folder, and package name, then runs cargo new with the same effective environment used by the extension. After creation, it can open the project, open it in a new window, or add it to the current VS Code workspace.

Details

  • Add the rust-analyzer.newProject command.
  • Add command palette, Explorer empty-state, and walkthrough entries.
  • Add rust-analyzer.projectCreation.openAfterCreate to configure the post-create action.
  • Validate project names before invoking Cargo for common input issues, while leaving Cargo as the source of truth for final package creation.
  • Clarify VS Code workspace wording in the post-create prompt.
  • Keep path display simple and avoid home-directory ~ shortening for now. That can be re-added later if wanted.

Testing

  • npm -C editors/code run typecheck
  • npm -C editors/code test
  • npm exec -- prettier src/new_project.ts tests/unit/commands.test.ts --check
  • npm exec -- eslint src/new_project.ts tests/unit/commands.test.ts

AI disclosure

I used Codex/AI tooling to research similar editor implementations, refine the implementation plan, review parts of the code structure, and draft/refine portions of this change. I reviewed the resulting code and notes before opening this PR.

Comment thread editors/code/src/commands.ts Outdated
Comment thread editors/code/src/commands.ts Outdated
Comment thread editors/code/src/commands.ts Outdated
Comment thread editors/code/src/commands.ts Outdated
Comment thread editors/code/src/commands.ts Outdated
Comment thread editors/code/src/toolchain.ts
Comment thread editors/code/src/toolchain.ts
@joshka joshka force-pushed the joshka/new-project-command branch from d7ff960 to 28f842e Compare April 20, 2026 02:43
@joshka

joshka commented Apr 20, 2026

Copy link
Copy Markdown
Contributor Author
image image image image image image image image

@joshka joshka marked this pull request as ready for review April 20, 2026 02:52
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 20, 2026
@ChayimFriedman2

Copy link
Copy Markdown
Contributor

This is a large wall of code, and I'm not even sure this feature is in scope for r-a.

@joshka

joshka commented Apr 20, 2026

Copy link
Copy Markdown
Contributor Author

This is a large wall of code, and I'm not even sure this feature is in scope for r-a.

It's working, self-reviewed, tested manually and with unit tests, with a firm spec in the linked issue that I spent time iterating on prior to throwing the tooling at it, plus refactoring and improving the generated code.

It exists because other extensions do this already, and it allows me to live in vscode rather than a terminal in order to create rust applications / libraries. This seems like a solidly good way to validate whether it's scope.

r-a is the place where rust projects live in vscode. From that perspective creating a new rust project in vscode should live in r-a.

Comment thread editors/code/src/commands.ts Outdated
Comment thread editors/code/src/commands.ts Outdated
Comment thread editors/code/src/commands.ts Outdated
@stefnotch

stefnotch commented Apr 22, 2026

Copy link
Copy Markdown

As a general question, which other extensions do this? I don't have any extensions installed with a similar feature, except for the Typst one. Though I don't fully understand the Typst one. I think Typst does it, because it doesn't rely on a CLI?

@joshka

joshka commented Apr 22, 2026

Copy link
Copy Markdown
Contributor Author

As a general question, which other extensions do this? I don't have any extensions installed with a similar feature, except for the Typst one. Though I don't fully understand the Typst one. I think Typst does it, because it doesn't rely on a CLI?

(From the issue plan details) #19338 (comment):
I also checked several other extensions to ground the expected behavior and tradeoffs here,
including Swift, Java, Python, Go, and .NET.

@joshka joshka force-pushed the joshka/new-project-command branch from f8c20bf to 1cb93f3 Compare April 23, 2026 17:10
@joshka joshka force-pushed the joshka/new-project-command branch from 70f7933 to 762b4e9 Compare May 6, 2026 06:54
@joshka

joshka commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

I pushed a cleanup pass aimed at making this easier to review: the new-project workflow now lives in editors/code/src/new_project.ts, with commands.ts only re-exporting the command. I also trimmed the helper docs down to the comments that explain behavior or tradeoffs.

Scope-wise, this still delegates project creation to cargo new; it does not edit Cargo manifests. The post-create "Add to VS Code Workspace" action only controls the VS Code workspace/window behavior.

@joshka joshka force-pushed the joshka/new-project-command branch from 762b4e9 to 585332d Compare May 6, 2026 07:00
@rustbot

This comment has been minimized.

@joshka joshka force-pushed the joshka/new-project-command branch from 585332d to 8ec8486 Compare May 6, 2026 07:12
@joshka

joshka commented May 6, 2026

Copy link
Copy Markdown
Contributor Author
Screen.Recording.2026-05-06.at.00.28.18.mov

@joshka

joshka commented May 13, 2026

Copy link
Copy Markdown
Contributor Author

ping

Implement a rust-analyzer VS Code command for creating new Cargo
projects from the editor. Add command registration, UI entry points,
configuration for post-create behavior, focused validation, and tests for
the command flow.

AI tools were used to research comparable editor behaviors and help
refine the implementation plan; the resulting changes were reviewed
before submission.
@joshka joshka force-pushed the joshka/new-project-command branch from 8ec8486 to 3376c4b Compare May 15, 2026 08:03
@rustbot

rustbot commented May 15, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@joshka

joshka commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

ping

@joshka

joshka commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready
r? @Veykril

(unsure if you're the best person to review this #t-compiler/rust-analyzer > New project command in VSCode @ 💬)

Point the new project failure flow at the extension logs and make the
log-opening command available when the language server is not running.
Also add tests for the env-aware cargo path resolution path introduced
for project creation.
@Veykril

Veykril commented May 25, 2026

Copy link
Copy Markdown
Member

Formatting failures

joshka added a commit to joshka/rust-analyzer that referenced this pull request May 25, 2026
Run the VS Code formatter to fix the remaining Prettier issue in the
launch config test before updating PR rust-lang#22103.

AI-assisted: OpenAI Codex was used to identify and apply this change.
@rustbot

This comment has been minimized.

joshka added a commit to joshka/rust-analyzer that referenced this pull request May 25, 2026
Run the VS Code formatter to fix the remaining Prettier issue in the
launch config test before updating rust-lang#22103.

AI-assisted: OpenAI Codex was used to identify and apply this change.
@joshka joshka force-pushed the joshka/new-project-command branch 2 times, most recently from be0c3f2 to 4af6a26 Compare May 25, 2026 21:17
Run the VS Code formatter to fix the remaining Prettier issue in the
launch config test before updating rust-lang#22103.

AI-assisted: OpenAI Codex was used to identify and apply this change.
@joshka joshka force-pushed the joshka/new-project-command branch 2 times, most recently from 9c34714 to c0a10a8 Compare May 25, 2026 21:45
@joshka

joshka commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

Iterating on the windows CI failure (I'm not actively testing on windows generally. Leaving that to CI here.

@joshka joshka force-pushed the joshka/new-project-command branch from c0a10a8 to 0ab0e38 Compare May 25, 2026 21:46
Handle Windows executable suffixes when the VS Code extension probes
for cargo through PATH or CARGO_HOME, and preserve case-insensitive
environment variable lookup for supplied env objects so copied Windows
`Path` entries still resolve cargo.

Normalize and document the Windows CARGO_HOME test comparison as well,
because VS Code path resolution can lowercase the drive letter in
`fsPath` without changing which executable was found.

This keeps the extension-side lookup aligned with the Rust-side
toolchain helper behavior and avoids breaking create-project on common
Windows setups.

AI-assisted: OpenAI Codex was used to identify and apply this change.
@joshka joshka force-pushed the joshka/new-project-command branch from 0ab0e38 to c690a64 Compare May 25, 2026 21:48
@joshka

joshka commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

The Windows CI failures on this PR turned up three separate issues in the VS Code-side cargo/toolchain resolution changes:

  1. We were only checking $CARGO_HOME/bin/cargo, not $CARGO_HOME/bin/cargo.exe, so the Windows CARGO_HOME lookup fell through and returned plain "cargo" instead of the resolved executable path.

  2. The new project flow passes a copied env object ({ ...process.env, ...serverExtraEnv }) into cargoPath(...). On Windows that commonly preserves the path variable as Path, not PATH, so the original supplied-env lookup could miss Cargo even when it was on the user’s PATH.

  3. After fixing the resolution logic, the remaining Windows failure was in the test itself: the assertion compared raw path strings, but the resolved fsPath came back with different drive-letter casing (c:\... vs C:\...). That was not a production bug, just a Windows path-normalization mismatch in the test.

@joshka

joshka commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@joshka

joshka commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

@Veykril can this be merged?

@Veykril Veykril added this pull request to the merge queue Jun 15, 2026
Merged via the queue into rust-lang:master with commit 888f581 Jun 15, 2026
19 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 15, 2026
@joshka

joshka commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Thanks!

lnicola pushed a commit to lnicola/rust that referenced this pull request Jun 23, 2026
Run the VS Code formatter to fix the remaining Prettier issue in the
launch config test before updating rust-lang/rust-analyzer#22103.

AI-assisted: OpenAI Codex was used to identify and apply this change.
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.

Create a new rust project from VSCode (cargo new / cargo init)

5 participants