-
Notifications
You must be signed in to change notification settings - Fork 248
[vscode-extension] reopen vscode in devbox env using process communication #1075
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
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ff1667c
Working demo for vscode env setup
mohsenari 9257b2b
Made improvements to process invoking
mohsenari de9ed20
Added env setup for integration in cli
mohsenari 39130bc
Some cleanup
mohsenari e8b519c
Addressing go lint
mohsenari c8f5384
removed time delays
mohsenari 9b34af1
Excluded env vars that caused issues
mohsenari 713d06e
Marked integrate command hidden
mohsenari f344724
Removed debug print statements
mohsenari 1ee4c81
Fixed issue with reopening vscode multiple times
mohsenari fcf924d
Remove commented code
mohsenari bb7d0fd
Removed more commented code
mohsenari fb387e1
Cleaned up and added better error handling
mohsenari 479ab21
more cleanup
mohsenari 4c9a41f
Added vscode subcommand to integrate
mohsenari b4c2f51
Removed hardcoded path to devbox
mohsenari 21115d5
Address linter and unsued command in package.json
mohsenari fbd7fb1
Added comment
mohsenari 073e926
debugging integrate command
mohsenari 146178a
Excluding devbox og path in integrate
mohsenari c2b45cc
removed debug command "integrate test"
mohsenari 327814a
fixed print env
mohsenari 6e1d7ca
Updated comment
mohsenari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved. | ||
// Use of this source code is governed by the license in the LICENSE file. | ||
|
||
package boxcli | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"os/exec" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/zealic/go2node" | ||
"go.jetpack.io/devbox" | ||
"go.jetpack.io/devbox/internal/debug" | ||
"go.jetpack.io/devbox/internal/impl/devopt" | ||
) | ||
|
||
type integrateCmdFlags struct { | ||
config configFlags | ||
} | ||
|
||
func integrateCmd() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "integrate", | ||
Short: "integrate with an IDE", | ||
Args: cobra.MaximumNArgs(1), | ||
Hidden: true, | ||
PreRunE: ensureNixInstalled, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cmd.Help() | ||
}, | ||
} | ||
command.AddCommand(integrateVSCodeCmd()) | ||
return command | ||
} | ||
|
||
func integrateVSCodeCmd() *cobra.Command { | ||
flags := integrateCmdFlags{} | ||
command := &cobra.Command{ | ||
Use: "vscode", | ||
Hidden: true, | ||
Short: "Integrate devbox environment with VSCode.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runIntegrateVSCodeCmd(cmd) | ||
}, | ||
} | ||
flags.config.register(command) | ||
|
||
return command | ||
} | ||
|
||
type parentMessage struct { | ||
ConfigDir string `json:"configDir"` | ||
} | ||
|
||
func runIntegrateVSCodeCmd(cmd *cobra.Command) error { | ||
|
||
// Setup process communication with node as parent | ||
channel, err := go2node.RunAsNodeChild() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get config dir as a message from parent process | ||
msg, err := channel.Read() | ||
if err != nil { | ||
return err | ||
} | ||
// Parse node process' message | ||
var message parentMessage | ||
if err = json.Unmarshal(msg.Message, &message); err != nil { | ||
return err | ||
} | ||
|
||
// todo: add error handling - consider sending error message to parent process | ||
box, err := devbox.Open(&devopt.Opts{ | ||
Dir: message.ConfigDir, | ||
Writer: cmd.OutOrStdout(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
// Get env variables of a devbox shell | ||
envVars, err := box.PrintEnvVars(cmd.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Send message to parent process to terminate | ||
err = channel.Write(&go2node.NodeMessage{ | ||
Message: []byte(`{"status": "finished"}`), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
// Open vscode with devbox shell environment | ||
cmnd := exec.Command("code", message.ConfigDir) | ||
cmnd.Env = append(cmnd.Env, envVars...) | ||
var outb, errb bytes.Buffer | ||
cmnd.Stdout = &outb | ||
cmnd.Stderr = &errb | ||
err = cmnd.Run() | ||
if err != nil { | ||
debug.Log("out: %s \n err: %s", outb.String(), errb.String()) | ||
return err | ||
} | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { window, workspace, commands, ProgressLocation, Uri } from 'vscode'; | ||
import { spawn, spawnSync } from 'node:child_process'; | ||
|
||
|
||
interface Message { | ||
status: string | ||
} | ||
export async function devboxReopen() { | ||
await window.withProgress({ | ||
location: ProgressLocation.Notification, | ||
title: "Setting up your Devbox environment. Please don't close vscode.", | ||
cancellable: true | ||
}, | ||
async (progress, token) => { | ||
token.onCancellationRequested(() => { | ||
console.log("User canceled the long running operation"); | ||
}); | ||
|
||
const p = new Promise<void>(async (resolve, reject) => { | ||
|
||
if (workspace.workspaceFolders) { | ||
const workingDir = workspace.workspaceFolders[0].uri; | ||
const dotdevbox = Uri.joinPath(workingDir, '/.devbox'); | ||
try { | ||
// check if .devbox exists | ||
await workspace.fs.stat(dotdevbox); | ||
} catch (error) { | ||
//.devbox doesn't exist | ||
// running devbox shellenv to create it | ||
spawnSync('devbox', ['shellenv'], { | ||
cwd: workingDir.path | ||
}); | ||
} | ||
// To use a custom compiled devbox when testing, change this to an absolute path. | ||
const devbox = 'devbox'; | ||
// run devbox integrate and then close this window | ||
let child = spawn(devbox, ['integrate', 'vscode'], { | ||
cwd: workingDir.path, | ||
stdio: [0, 1, 2, 'ipc'] | ||
}); | ||
// if CLI closes before sending "finished" message | ||
child.on('close', (code: number) => { | ||
console.log("child process closed with exit code:", code); | ||
window.showErrorMessage("Failed to setup devbox environment."); | ||
reject(); | ||
}); | ||
// send config path to CLI | ||
child.send({ configDir: workingDir.path }); | ||
// handle CLI finishing the env and sending "finished" | ||
child.on('message', function (msg: Message, handle) { | ||
if (msg.status === "finished") { | ||
resolve(); | ||
commands.executeCommand("workbench.action.closeWindow"); | ||
} | ||
}); | ||
} | ||
}); | ||
return p; | ||
} | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would have to be something else for prod?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LucilleH No, for testing locally on a compiled devbox this would have to point to the path to the compiled devbox.
This comment is mostly a note-to-self for future changes.
For example, if I change something in CLI and want to see how it interacts with the extension, I should change this line to
/Users/mohsen/projects/devbox/dist/devbox
so that my changes in compiled devbox CLI gets used instead of the installed devbox. Hope that makes sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah,
To use a custom compiled devbox when testing, change this to an absolute path.
lol