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

Allow a default ".code-workspace" file to open to be specified in devcontainer.json #1124 #3665

Open
egamma opened this issue Sep 14, 2020 · 31 comments
Assignees
Labels
containers Issue in vscode-remote containers feature-request Request for new features or functionality
Milestone

Comments

@egamma
Copy link
Member

egamma commented Sep 14, 2020

There has been a surprising amount of interest in having devcontainer.json reference and open a .code-workspace by default when reopen folder in container using Remote - Containers or in a codespace. While there is a workaround, it breaks the "it's ready to go when you create a dev container" promise.

@egamma egamma added feature-request Request for new features or functionality containers Issue in vscode-remote containers labels Sep 14, 2020
@egamma egamma added this to the September 2020 milestone Sep 14, 2020
@chrmarti chrmarti added plan-item A plan item and removed feature-request Request for new features or functionality labels Sep 15, 2020
@chrmarti chrmarti added feature-request Request for new features or functionality and removed plan-item A plan item labels Sep 29, 2020
@chrmarti chrmarti removed this from the September 2020 milestone Sep 29, 2020
@dslijepcevic
Copy link

@egamma I'm curious, what would be the current workaround for automating this that you are referring to? Is it somehow possible to call remote-containers.openFolder with an uri parameter from a dummy folder opened in order to build the container?

Another thing that would go hand in hand with this feature would be the ability to specify .devcontainer.json location in VSCode settings so that once you auto open your workspace file (which might be in an arbitrary location within the container) any modifications to the container image get picked up. Right now I'm symlinking the needed files in a generated workspace file's directory, but that seems kind of hacky (details here).

@fabiosimeoni
Copy link

fabiosimeoni commented Jan 12, 2022

Here's a specific use case:

  • the multi-root workspace lives exclusively in the container, nothing makes it to the host.

  • the projects in the workspace come from two different named volumes:

    • one app volume with application components and the .code-workspace file.
    • one libs volume with with shared libraries (this can be mounted on other containers too, hence the split).
  • the app volume is the workspaceMountFolder, the libs volume is mounted elsewhere.

  • the .code-workspace file brings together projects from both volumes in the flat layout of multi-root workspaces.

The transition from host to container would be seamless if the .code-workspace in the workspaceMountFolder opened automatically.

@sarg3nt
Copy link

sarg3nt commented Mar 22, 2022

@dslijepcevic I see that @egamma never got back to you. Did you ever figure out how to auto open a code-workspace file when the devcontainer opens?
Thanks!

@sarg3nt
Copy link

sarg3nt commented Mar 22, 2022

@chrmarti This got unassigned from the Sep 2020 milestone and then kind of looks like it got buried as it's been 2 years now. Is the intent to get this done at some point, or should I give up?

@chrmarti
Copy link
Contributor

@sarg3nt The feature request makes sense. It just hasn't made it on our plans, so we don't have an ETA. (There are workarounds: Open the multi-root workspace locally and then reopen in container or the other way around.)

@chrmarti chrmarti added this to the Backlog milestone Mar 28, 2022
@mririgoyen
Copy link

mririgoyen commented Apr 6, 2022

I really hope this makes it into your plans soon. Monorepos could really benefit from this and it's the only thing holding a project of mine back from making the switch to devcontainers.

@holdemir
Copy link

this would be very helpfull :)

@ericchansen
Copy link

I am a big fan of this. Pleeeeeease make it happen.

@chuanqisun
Copy link

chuanqisun commented Jun 10, 2022

+1
My team got really confused when they had to do the triple jump: local -> ssh -> docker -> workspace
People accidentally open workspace before opening docker container and that is very difficult to back out. The issue also compounds when it leaves a record in the "Open Recent" list. Even after one successfully performed the triple jump, it's likely in the future they will accidentally open the workspace (outside of docker) by picking the wrong item in the "Open Recent" list.

Please consider supporting a default workspace in the devcontainer.json.

Thanks!

@cor
Copy link

cor commented Jul 18, 2022

Any update on this? We really need this (and would gladly pay a bounty for making this happen)

@and-pete
Copy link

An enthusiastic +1 to this from me also 😃

@chrmarti
Copy link
Contributor

We have since moved all VS Code specific properties to "customizations"."vscode" in the devcontainer.json. We could add the workspace file's path there. The Dev Containers extension would then read it when reopening the window.

@souryavarenya
Copy link

@chrmarti Can you mention which settings in customizations.vscode.settings let you do this?

@slimcdk
Copy link

slimcdk commented Jan 15, 2023

@chrmarti I'm also interested in the answer to @souryavarenyas question.

@JesseRigon
Copy link

The OP states "a surprising amount of requests" but this shouldn't be surprising if you've ever used gitpod, a competitor to codespaces. Gitpods equivalent to Github Codespaces' .devcontainer.json is called .gitpod.yml and you can specify a .code-workspace file. This is really useful in combination with the .gitpod.yml as you can auto clone other repositories into your container and it will add those folders to vscode file explorer which makes working with multi-repo projects extremely nice. I personally have (private) repos with all my devenv configuration stuff so that it doesn't crowd my (public or private) project repos.

Multi Repos hasn't been a focus of codespaces until recently (at least publically) so I hope that this added use case gives some clarity to the increase in demand.

@ygini
Copy link

ygini commented Feb 2, 2023

@souryavarenya @slimcdk under customizations.vscode you can put the content of your whole code-workspace file basically (settings, folders, extensions, etc.)

This replace the code-workspace for your devcontainer

@chrmarti
Copy link
Contributor

chrmarti commented Feb 3, 2023

customizations.vscode only supports settings and extensions at the moment. The request to support a .code-workspace file still makes sense.

@ygini
Copy link

ygini commented Feb 3, 2023

@chrmarti folders works too

@tonynajjar
Copy link

@chrmarti folders works too

image

doesn't for me

@souryavarenya
Copy link

For me, tasks and launch are the important ones.

@dslatkin
Copy link

+1 to having a way to specify a .code-workspace file to automatically open a workspace when connecting to a container. Everything else is trivially easy to get up and running in our project with the one exception of finding and opening (in our case) a multi-root workspace which is essential to our workflow.

elenatanasoiu added a commit to github/vscode-codeql that referenced this issue Mar 20, 2023
When opening https://github.com/github/codespaces-codeql/ in a
codespace, it's easy to miss the prompt that tells you to open the
tutorial.code-workspace file.

In fact people actively dismiss the alert to get it out of the way.

If you miss that prompt, you end up with a single-rooted workspace,
which causes various other problems.

While there is an open issue to allow VS Code to open a default
workspace [1], there doesn't seem to have been any progress on it
in the last two years.

So we're taking matters into our own hands and forcing the extension
to open the tutorial workspace, if it detects it.

This will only happen if the following three conditions are met:
- the .tours folder exists
- the tutorial.code-workspace file exists
- the CODESPACES_TEMPLATE setting hasn't been set

NB: the `CODESPACES_TEMPLATE` setting can only be found if the
tutorial.code-workspace has already been opened. So it's a good
indicator that we're in the folder, but the user has ignored the prompt.

[1]: microsoft/vscode-remote-release#3665
elenatanasoiu added a commit to github/vscode-codeql that referenced this issue Mar 20, 2023
When opening https://github.com/github/codespaces-codeql/ in a
codespace, it's easy to miss the prompt that tells you to open the
tutorial.code-workspace file.

In fact people actively dismiss the alert to get it out of the way.

If you miss that prompt, you end up with a single-rooted workspace,
which causes various other problems.

While there is an open issue to allow VS Code to open a default
workspace [1], there doesn't seem to have been any progress on it
in the last two years.

So we're taking matters into our own hands and forcing the extension
to open the tutorial workspace, if it detects it.

This will only happen if the following three conditions are met:
- the .tours folder exists
- the tutorial.code-workspace file exists
- the CODESPACES_TEMPLATE setting hasn't been set

NB: the `CODESPACES_TEMPLATE` setting can only be found if the
tutorial.code-workspace has already been opened. So it's a good
indicator that we're in the folder, but the user has ignored the prompt.

[1]: microsoft/vscode-remote-release#3665
elenatanasoiu added a commit to github/vscode-codeql that referenced this issue Mar 20, 2023
When opening https://github.com/github/codespaces-codeql/ in a
codespace, it's easy to miss the prompt that tells you to open the
tutorial.code-workspace file.

In fact people actively dismiss the alert to get it out of the way.

If you miss that prompt, you end up with a single-rooted workspace,
which causes various other problems.

While there is an open issue to allow VS Code to open a default
workspace [1], there doesn't seem to have been any progress on it
in the last two years.

So we're taking matters into our own hands and forcing the extension
to open the tutorial workspace, if it detects it.

This will only happen if the following three conditions are met:
- the .tours folder exists
- the tutorial.code-workspace file exists
- the CODESPACES_TEMPLATE setting hasn't been set

NB: the `CODESPACES_TEMPLATE` setting can only be found if the
tutorial.code-workspace has already been opened. So it's a good
indicator that we're in the folder, but the user has ignored the prompt.

[1]: microsoft/vscode-remote-release#3665
elenatanasoiu added a commit to github/vscode-codeql that referenced this issue Mar 20, 2023
When opening https://github.com/github/codespaces-codeql/ in a
codespace, it's easy to miss the prompt that tells you to open the
tutorial.code-workspace file.

In fact people actively dismiss the alert to get it out of the way.

If you miss that prompt, you end up with a single-rooted workspace,
which causes various other problems.

While there is an open issue to allow VS Code to open a default
workspace [1], there doesn't seem to have been any progress on it
in the last two years.

So we're taking matters into our own hands and forcing the extension
to open the tutorial workspace, if it detects it.

This will only happen if the following three conditions are met:
- the .tours folder exists
- the tutorial.code-workspace file exists
- the CODESPACES_TEMPLATE setting hasn't been set

NB: the `CODESPACES_TEMPLATE` setting can only be found if the
tutorial.code-workspace has already been opened. So it's a good
indicator that we're in the folder, but the user has ignored the prompt.

[1]: microsoft/vscode-remote-release#3665
elenatanasoiu added a commit to github/vscode-codeql that referenced this issue Mar 20, 2023
When opening https://github.com/github/codespaces-codeql/ in a
codespace, it's easy to miss the prompt that tells you to open the
tutorial.code-workspace file.

In fact people actively dismiss the alert to get it out of the way.

If you miss that prompt, you end up with a single-rooted workspace,
which causes various other problems.

While there is an open issue to allow VS Code to open a default
workspace [1], there doesn't seem to have been any progress on it
in the last two years.

So we're taking matters into our own hands and forcing the extension
to open the tutorial workspace, if it detects it.

This will only happen if the following three conditions are met:
- the .tours folder exists
- the tutorial.code-workspace file exists
- the CODESPACES_TEMPLATE setting hasn't been set

NB: the `CODESPACES_TEMPLATE` setting can only be found if the
tutorial.code-workspace has already been opened. So it's a good
indicator that we're in the folder, but the user has ignored the prompt.

[1]: microsoft/vscode-remote-release#3665
@chris-boson
Copy link

Any movement on this? Would really appreciate this, since it causes situations that are hard to recover from for inexperienced users.

@janhurst
Copy link

@chrmarti folders works too

image

doesn't for me

I found this thread trying to understand how to get a .code-workspace file to be opened by default in a devcontainer, but I actually think being able to manipulate the folders "config" (?) is what I am looking for.... apart from the monorepo type use case, I am also trying to add a azure storage container via the Microsoft vscode Azure extensions

@dslatkin
Copy link

dslatkin commented Jul 11, 2023

Warning

I think the output of code --status changed to longer make it possible to detect which windows are open, making this no longer a viable workaround.

Original comment

After a lot of iteration, for anybody that wants a workaround to make opening the workspace basically foolproof for people using the container, I've figured out what I think is actually a decent way to reopen the container in the workspace automatically the first time an interactive shell is opened after the container started, whether or not it's in a workspace.

Make sure to replace <project-name> with your project name as you want it to appear in VS Code's title bar and recently opened list.

First, I have my devcontainer.json looking like this. I use ${containerWorkspaceFolder} so the installed folder on the host machine doesn't matter:

{
    // ...
    "containerEnv": {
        "WORKSPACE_FILE": "${containerWorkspaceFolder}/<project-name>.code-workspace",
        "WORKSPACE_BASHRC": "${containerWorkspaceFolder}/.devcontainer/bashrc.sh"
    },
    "postCreateCommand": "${containerWorkspaceFolder}/.devcontainer/postCreateScript.sh",
    "postStartCommand": "${containerWorkspaceFolder}/.devcontainer/postStartScript.sh",
    // ...
}

Inside postCreateScript.sh, I append the user's .bashrc to make sure my own script gets run for every new interactive shell:

#!/bin/bash

set -euo pipefail

cat <<EOF >> ~/.bashrc
# Generated from dev container in $(basename "$0")
source "\$WORKSPACE_BASHRC"
EOF

Inside postStartScript.sh, since I want this to make sure the workspace is checked every time the container is started, I clean up a file I use to check that:

#!/bin/bash

set -euo pipefail

rm --force ~/.workspace-opened

Finally, in bashrc.sh, if that file is:

  1. not yet present, ensuring the workspace is checked once every time the container starts but not on every single interactive shell and
  2. the workspace is not currently open, in case the dev container has been started directly in the workspace, e.g. from the "Recents" menu

I reopen the current non-workspace container in the workspace:

check_workspace () {
    workspace_base_filename=$(basename "$WORKSPACE_FILE")
    workspace_name=${workspace_base_filename%.*}
    current_windows=$(code --status | grep --extended-regexp "window \[[0-9]+\]")
    if ! echo "$current_windows" | grep --fixed-strings --quiet "$workspace_name (Workspace)"; then
        if ! code --reuse-window "$WORKSPACE_FILE"; then
            echo "Failed to open workspace" >&2
            return 1
        fi
    fi
    
    touch ~/.workspace-opened
}

if ! [ -f ~/.workspace-opened ]; then
    check_workspace
fi

It would be really nice to have this as a feature some day and not need to jump through these hoops. We heavily depend on multi-root workspaces and we can't find another way to make things nice with dev containers otherwise.

@Kaptensanders
Copy link

I'm a bit new to devcontainers but I solved multi root like this, not sure if there is a problem with this approach

select/create a project folder (docker volume probably) and mount everything you want there. Then set it as workspacefolder. like:

 "mounts": [
    "source=/.../folder1,target=/projectFolder/folder1,type=bind",
    "source=/.../folder2/,target=/projectFolder/folder2,type=bind",
],
"workspaceFolder": "/projectFolder",

I always open the container in vscode using CLI, devcontainer open

@dslatkin
Copy link

dslatkin commented Sep 9, 2023

not sure if there is a problem with this approach

@Kaptensanders In your setup, I'm guessing you have devcontainer.json version controlled in one of the nested folders i.e. either folder1 or folder2 and you don't need any other multi-root VS Code features specific to those nested folders?

Since you're not actually opening a workspace file with what you've shared and I think setting workspaceFolder just treats it implicitly as a single-folder workspace, you would be missing improved multi-root workspace support for explorer, search, command palette, git, etc. for each of the subfolders. It would be more or less the same thing as having /projectFolder be the git repo and putting devcontainer.json in that and not specifying workspaceFolder.

@Kaptensanders
Copy link

Kaptensanders commented Sep 10, 2023

not sure if there is a problem with this approach

@Kaptensanders In your setup, I'm guessing you have devcontainer.json version controlled in one of the nested folders i.e.

Yes, I open vscode with devcontainer open from within the subfolder/repo that contains .devcontainer/devcontainer.json.

and you don't need any other multi-root VS Code features specific to those nested folders?
As it is a backend-frontend setup split into two different repos it would be nice, but no. Probably I could specify some support for building project2 in project1/.vscode folder, but no I don't really need it.

Since you're not actually opening a workspace file with what you've shared and I think setting workspaceFolder just treats it implicitly as a single-folder workspace, you would be missing improved multi-root workspace support for explorer, search, command palette, git, etc. for each of the subfolders.

supported vscode customizations from the devcontainer.json are loaded and for tasks etc to work, I mount the .vscode folder in the workspaceFolder root

It would be more or less the same thing as having /projectFolder be the git repo and putting devcontainer.json in that and not specifying workspaceFolder.

Not sure, haven't tried. But without setting the workspaceFolder to the repos parent, how would I get vscode to show me the other mounted folders?

U can see it here
https://github.com/Kaptensanders/skolmat/blob/main/.devcontainer/devcontainer.json

It works as a hack substitute for real .code-workspace support. I'm sure I'm missing out on some things but I'm not the most advanced user.

@floge07
Copy link

floge07 commented Sep 10, 2023

Not sure if this is useful to everyone, but I think I found a reasonable solution.

Install this extension in the dev container:
image

Documentation to that Extension.

Now, add in the root directory of your workspace a .vscode folder with a settings.json:
image

The result is: If someone opens the dev container normally, the settings.json in root is used, the setting tells the extension to auto-open the first code-workspace file and the extension does that.

Still not that nice... After all, everything gets loaded, then the extension gets initialized, opens the code workspace, everything gets unloaded, and then everything gets loaded/initialized one final time.

@Kaptensanders
Copy link

After some serious head scratching I put together a script that fixes this, it opens a vscode workspace within a devcontainer.
https://gist.github.com/Kaptensanders/79da7c1547751fb43c75904e3110bbf9

@elliottAtTreatment
Copy link

Anyone here about any progress for this? The script is nice, but we're not really looking to implement a workaround we'll have to maintain.

@RollsChris
Copy link

I'm also looking for this feature, every time i open my DEV container i then have to go and select the workspace file to use...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
containers Issue in vscode-remote containers feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests