Skip to content

Commit e25af69

Browse files
author
shijiashuai
committed
fix: 修复 Dev Container 中 .gitconfig 注入路径问题,改为挂载临时路径并在创建阶段复制
1 parent 0040842 commit e25af69

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"overrideCommand": true,
1414
"mounts": [
1515
"type=bind,source=${localEnv:HOME}/.ssh,target=/home/developer/.ssh,readonly",
16-
"type=bind,source=${localEnv:HOME}/.gitconfig,target=/home/developer/.gitconfig,readonly",
16+
"type=bind,source=${localEnv:HOME}/.gitconfig,target=/tmp/host-gitconfig,readonly",
1717
"type=bind,source=${localEnv:SSH_AUTH_SOCK},target=/ssh-agent"
1818
],
1919
"remoteEnv": {
@@ -23,7 +23,7 @@
2323
"forwardPorts": [
2424
8080
2525
],
26-
"postCreateCommand": "bash -lc \"if [ -z \\\"${WSL_DISTRO_NAME:-}\\\" ]; then echo 'WARNING: 建议使用 VS Code Remote - WSL 在 WSL2 中打开仓库,再执行 Reopen in Container(更稳定/更快/更一致)。' >&2; fi; if [ ! -S /ssh-agent ]; then echo 'WARNING: 未检测到 SSH agent 注入(/ssh-agent 不存在)。若需要访问私有仓库,请在 WSL 中执行:eval \\\"$(ssh-agent -s)\\\" && ssh-add,然后 Rebuild/Reopen。' >&2; fi; git config commit.template .gitmessage.txt || true; conan profile detect --force >/dev/null 2>&1 || true\"",
26+
"postCreateCommand": "bash -lc \"cd /workspace || exit 0; if [ -z \\\"${WSL_DISTRO_NAME:-}\\\" ]; then echo 'WARNING: 建议使用 VS Code Remote - WSL 在 WSL2 中打开仓库,再执行 Reopen in Container(更稳定/更快/更一致)。' >&2; fi; if [ ! -S /ssh-agent ]; then echo 'WARNING: 未检测到 SSH agent 注入(/ssh-agent 不存在)。若需要访问私有仓库,请在 WSL 中执行:eval \\\"$(ssh-agent -s)\\\" && ssh-add,然后 Rebuild/Reopen。' >&2; fi; if [ -d /home/developer/.gitconfig ]; then sudo rm -rf /home/developer/.gitconfig; fi; if [ -f /tmp/host-gitconfig ]; then cp -f /tmp/host-gitconfig /home/developer/.gitconfig; fi; if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then git config commit.template .gitmessage.txt || true; else echo 'WARNING: /workspace 不是 Git 仓库,跳过 git config commit.template。' >&2; fi; conan profile detect --force >/dev/null 2>&1 || true\"",
2727
"customizations": {
2828
"vscode": {
2929
"settings": {

.devcontainer/devcontainer.simple.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@
88
"workspaceFolder": "/workspace",
99
"remoteUser": "developer",
1010
"overrideCommand": true,
11-
"postCreateCommand": "bash -lc \"git config commit.template .gitmessage.txt || true; conan profile detect --force >/dev/null 2>&1 || true\"",
11+
"mounts": [
12+
"type=bind,source=${localEnv:HOME}/.ssh,target=/home/developer/.ssh,readonly",
13+
"type=bind,source=${localEnv:HOME}/.gitconfig,target=/tmp/host-gitconfig,readonly",
14+
"type=bind,source=${localEnv:SSH_AUTH_SOCK},target=/ssh-agent"
15+
],
16+
"remoteEnv": {
17+
"SSH_AUTH_SOCK": "/ssh-agent",
18+
"WSL_DISTRO_NAME": "${localEnv:WSL_DISTRO_NAME}"
19+
},
20+
"forwardPorts": [
21+
8080
22+
],
23+
"postCreateCommand": "bash -lc \"cd /workspace || exit 0; if [ -z \\\"${WSL_DISTRO_NAME:-}\\\" ]; then echo 'WARNING: 建议使用 VS Code Remote - WSL 在 WSL2 中打开仓库,再执行 Reopen in Container(更稳定/更快/更一致)。' >&2; fi; if [ ! -S /ssh-agent ]; then echo 'WARNING: 未检测到 SSH agent 注入(/ssh-agent 不存在)。若需要访问私有仓库,请在 WSL 中执行:eval \\\"$(ssh-agent -s)\\\" && ssh-add,然后 Rebuild/Reopen。' >&2; fi; if [ -d /home/developer/.gitconfig ]; then sudo rm -rf /home/developer/.gitconfig; fi; if [ -f /tmp/host-gitconfig ]; then cp -f /tmp/host-gitconfig /home/developer/.gitconfig; fi; if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then git config commit.template .gitmessage.txt || true; else echo 'WARNING: /workspace 不是 Git 仓库,跳过 git config commit.template。' >&2; fi; conan profile detect --force >/dev/null 2>&1 || true\"",
1224
"customizations": {
1325
"vscode": {
1426
"settings": {

changelog/2026-01-08-devcontainer-windows.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- 增加对 WSL2 打开方式的校验:要求通过 VS Code Remote - WSL 打开工作区后再 Reopen in Container。
1212
- 增加 SSH/Git 自动注入:挂载宿主机(WSL)侧 `~/.ssh``~/.gitconfig`,并转发 `SSH_AUTH_SOCK`
1313
- 将 WSL2/SSH 校验由“强制失败”调整为“仅提示”(warning),避免将工作流规定写死为硬阻断。
14+
- 修复 `.gitconfig` 注入导致容器内路径变成目录的问题:改为挂载到临时路径并在创建阶段复制到用户目录;同时避免在非 Git 仓库目录执行 `git config` 报错。
1415
- `scripts/devcontainer_setup.sh`:修复在 `set -u` 模式下引用未定义环境变量(`GIT_USER_NAME` / `GIT_USER_EMAIL`)导致脚本退出的问题。
1516
- 文档:新增 `docs/dev/devcontainer-guidelines.md` 作为团队 Dev Container 开发规范,并在 `README.md` / `docs/README.md` 中增加入口链接。
1617

0 commit comments

Comments
 (0)