Skip to content

feat: locate devcontainer.json in multiple places #134

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

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 77 additions & 16 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ type Options struct {
// DevcontainerDir. This can be used in cases where one wants
// to substitute an edited devcontainer.json file for the one
// that exists in the repo.
// If neither `DevcontainerDir` nor `DevcontainerJSONPath` is provided,
// envbuilder will browse following directories to locate it:
// 1. `.devcontainer/devcontainer.json`
// 2. `.devcontainer.json`
// 3. `.devcontainer/<folder>/devcontainer.json`
DevcontainerJSONPath string `env:"DEVCONTAINER_JSON_PATH"`

// DockerfilePath is a relative path to the Dockerfile that
Expand Down Expand Up @@ -422,22 +427,11 @@ func Run(ctx context.Context, options Options) error {
if options.DockerfilePath == "" {
// Only look for a devcontainer if a Dockerfile wasn't specified.
// devcontainer is a standard, so it's reasonable to be the default.
devcontainerDir := options.DevcontainerDir
if devcontainerDir == "" {
devcontainerDir = ".devcontainer"
}
if !filepath.IsAbs(devcontainerDir) {
devcontainerDir = filepath.Join(options.WorkspaceFolder, devcontainerDir)
}
devcontainerPath := options.DevcontainerJSONPath
if devcontainerPath == "" {
devcontainerPath = "devcontainer.json"
}
if !filepath.IsAbs(devcontainerPath) {
devcontainerPath = filepath.Join(devcontainerDir, devcontainerPath)
}
_, err := options.Filesystem.Stat(devcontainerPath)
if err == nil {
devcontainerPath, devcontainerDir, err := findDevcontainerJSON(options)
if err != nil {
logf(codersdk.LogLevelError, "Failed to locate devcontainer.json: %s", err.Error())
logf(codersdk.LogLevelError, "Falling back to the default image...")
} else {
// We know a devcontainer exists.
// Let's parse it and use it!
file, err := options.Filesystem.Open(devcontainerPath)
Expand Down Expand Up @@ -1201,3 +1195,70 @@ type osfsWithChmod struct {
func (fs *osfsWithChmod) Chmod(name string, mode os.FileMode) error {
return os.Chmod(name, mode)
}

func findDevcontainerJSON(options Options) (string, string, error) {
// 0. Check if custom devcontainer directory or path is provided.
if options.DevcontainerDir != "" || options.DevcontainerJSONPath != "" {
devcontainerDir := options.DevcontainerDir
if devcontainerDir == "" {
devcontainerDir = ".devcontainer"
}
// If `devcontainerDir` is not an absolute path, assume it is relative to the workspace folder.
if !filepath.IsAbs(devcontainerDir) {
devcontainerDir = filepath.Join(options.WorkspaceFolder, devcontainerDir)
}

// An absolute location always takes a precedence.
devcontainerPath := options.DevcontainerJSONPath
if filepath.IsAbs(devcontainerPath) {
return options.DevcontainerJSONPath, devcontainerDir, nil
}
// If an override is not provided, assume it is just `devcontainer.json`.
if devcontainerPath == "" {
devcontainerPath = "devcontainer.json"
}

if !filepath.IsAbs(devcontainerPath) {
devcontainerPath = filepath.Join(devcontainerDir, devcontainerPath)
}
return devcontainerPath, devcontainerDir, nil
}

// 1. Check `options.WorkspaceFolder`/.devcontainer/devcontainer.json.
location := filepath.Join(options.WorkspaceFolder, ".devcontainer", "devcontainer.json")
if _, err := options.Filesystem.Stat(location); err == nil {
return location, filepath.Dir(location), nil
}

// 2. Check `options.WorkspaceFolder`/devcontainer.json.
location = filepath.Join(options.WorkspaceFolder, "devcontainer.json")
if _, err := options.Filesystem.Stat(location); err == nil {
return location, filepath.Dir(location), nil
}

// 3. Check every folder: `options.WorkspaceFolder`/.devcontainer/<folder>/devcontainer.json.
devcontainerDir := filepath.Join(options.WorkspaceFolder, ".devcontainer")

fileInfos, err := options.Filesystem.ReadDir(devcontainerDir)
if err != nil {
return "", "", err
}

logf := options.Logger
for _, fileInfo := range fileInfos {
if !fileInfo.IsDir() {
logf(codersdk.LogLevelDebug, `%s is a file`, fileInfo.Name())
continue
}

location := filepath.Join(devcontainerDir, fileInfo.Name(), "devcontainer.json")
if _, err := options.Filesystem.Stat(location); err != nil {
logf(codersdk.LogLevelDebug, `stat %s failed: %s`, location, err.Error())
continue
}

return location, filepath.Dir(location), nil
}

return "", "", errors.New("can't find devcontainer.json, is it a correct spec?")
}
47 changes: 47 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,53 @@ func TestBuildFromDevcontainerInCustomPath(t *testing.T) {
require.Equal(t, "hello", strings.TrimSpace(output))
}

func TestBuildFromDevcontainerInSubfolder(t *testing.T) {
t.Parallel()

// Ensures that a Git repository with a devcontainer.json is cloned and built.
url := createGitServer(t, gitServerOptions{
files: map[string]string{
".devcontainer/subfolder/devcontainer.json": `{
"name": "Test",
"build": {
"dockerfile": "Dockerfile"
},
}`,
".devcontainer/subfolder/Dockerfile": "FROM ubuntu",
},
})
ctr, err := runEnvbuilder(t, options{env: []string{
"GIT_URL=" + url,
}})
require.NoError(t, err)

output := execContainer(t, ctr, "echo hello")
require.Equal(t, "hello", strings.TrimSpace(output))
}
func TestBuildFromDevcontainerInRoot(t *testing.T) {
t.Parallel()

// Ensures that a Git repository with a devcontainer.json is cloned and built.
url := createGitServer(t, gitServerOptions{
files: map[string]string{
"devcontainer.json": `{
"name": "Test",
"build": {
"dockerfile": "Dockerfile"
},
}`,
"Dockerfile": "FROM ubuntu",
},
})
ctr, err := runEnvbuilder(t, options{env: []string{
"GIT_URL=" + url,
}})
require.NoError(t, err)

output := execContainer(t, ctr, "echo hello")
require.Equal(t, "hello", strings.TrimSpace(output))
}

func TestBuildCustomCertificates(t *testing.T) {
srv := httptest.NewTLSServer(createGitHandler(t, gitServerOptions{
files: map[string]string{
Expand Down
Loading