Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion desktop/internal/core/remote_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@ func (c *Client) RemoteWriteFile(ctx context.Context, sessionID, path string, da
return result.RemoteFileVersion, true, nil
}

// RemoteCreateDirectory creates the host folder path over the session sessionID with its missing
// parents; a folder already there is left as it is. RemoteWriteFile creates no parent, so a file
// whose folder may be missing is written after it.
func (c *Client) RemoteCreateDirectory(ctx context.Context, sessionID, path string) error {
return c.RemoteHostRequest(ctx, sessionID, "fs.createDir", struct {
Path string `json:"path"`
Recursive bool `json:"recursive"`
}{path, true}, nil)
}

// RemoteHostRequest performs the finite host method with params over the session sessionID and
// decodes its result into result, which may be nil. Core accepts the filesystem, environment
// and port queries, server.state and the controls of existing processes and terminals; a
Expand Down Expand Up @@ -395,19 +405,22 @@ func WithRemoteWorkspace(ctx context.Context, binding *RemoteWorkspace) context.
// that will then fail. workspace.filterChanges drops the host watch's changes in folders Core
// hides, by name alone; workspace.searchPresentation groups search results by the paths it is
// sent, and workspace.replaceText replaces in the text it is sent, which Replace All read from
// the host.
// the host. editor.find searches the text of the editor or the diff side it is sent, and
// github.parseRemote reads the GitHub repository out of the remote URL the host's Git answered.
//
// Add a command here only after reading its Core handler; a command that touches the workspace
// sent without the binding would read the empty placeholder instead of the host.
var payloadOnlyCommands = map[string]bool{
"document.lifecycle": true,
"document.planSaveBatch": true,
"document.reduceSaveBatch": true,
"editor.find": true,
"editor.lineEdit": true,
"editor.navigationHistory.reduce": true,
"editor.parseNavigationInput": true,
"editor.recent.reduce": true,
"editor.recovery.decide": true,
"github.parseRemote": true,
"languages.matchFirstLine": true,
"markdown.outline": true,
"text.decode": true,
Expand Down
12 changes: 8 additions & 4 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions desktop/internal/remoteplaceholder/remoteplaceholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,59 @@ func reservedOnWindows(name string) bool {
}
return false
}

// HostSuffix is added to a local root to name the sibling Core maps every host path outside the
// workspace to: <root>.lithe-remote-host/<the host path without its leading "/">.
const HostSuffix = ".lithe-remote-host"

// HostPath is the host path local stands for, as Core maps it, in the workspace whose local root
// is root and whose host folder is remoteRoot: a path below root is the same path below
// remoteRoot, and a path below root's sibling (HostSuffix) is the host path it names. ok is false
// for any other path.
func HostPath(root, remoteRoot, local string) (string, bool) {
if suffix, ok := below(root+HostSuffix, local); ok {
return path.Join("/", suffix), true
}
if suffix, ok := below(root, local); ok {
return path.Join(remoteRoot, suffix), true
}
return "", false
}

// LocalPath is the path that stands for host, an absolute host path, in the same workspace: the
// inverse of HostPath. ok is false for a path that is not absolute, and on Windows for one with a
// name holding "\", which would name another path there.
func LocalPath(root, remoteRoot, host string) (string, bool) {
if !path.IsAbs(host) {
return "", false
}
host = path.Clean(host)
remote := path.Clean(remoteRoot)
if host == remote {
return root, true
}
base, suffix := root+HostSuffix, strings.TrimPrefix(host, "/")
if rest, ok := strings.CutPrefix(host, strings.TrimSuffix(remote, "/")+"/"); ok {
base, suffix = root, rest
}
if suffix == "" {
return base, true
}
if filepath.Separator != '/' && strings.ContainsRune(suffix, filepath.Separator) {
return "", false
}
return filepath.Join(base, filepath.FromSlash(suffix)), true
}

// below is the slash-separated path of full below dir, "" for dir itself; ok is false for a path
// outside dir.
func below(dir, full string) (string, bool) {
rel, err := filepath.Rel(dir, full)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", false
}
if rel == "." {
return "", true
}
return filepath.ToSlash(rel), true
}
35 changes: 35 additions & 0 deletions desktop/internal/remoteplaceholder/remoteplaceholder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,38 @@ func TestRootNamesTheFolderAndTellsWorkspacesApart(t *testing.T) {
}
}
}

// HostPath and LocalPath map the workspace's own paths below the root and every other host path
// below the root's sibling, as Core does, and each undoes the other.
func TestHostAndLocalPathsMapBothWays(t *testing.T) {
anchor := filepath.Join(string(filepath.Separator)+"cache", AnchorName)
root := Root(anchor, "c1", "/srv/repo")
sibling := root + HostSuffix
for _, tc := range []struct{ host, local string }{
{"/srv/repo", root},
{"/srv/repo/.git/HEAD", filepath.Join(root, ".git", "HEAD")},
{"/srv/main/.git/worktrees/repo", filepath.Join(sibling, "srv", "main", ".git", "worktrees", "repo")},
{"/srv/repository/x", filepath.Join(sibling, "srv", "repository", "x")},
{"/", sibling},
} {
local, ok := LocalPath(root, "/srv/repo", tc.host)
if !ok || local != tc.local {
t.Errorf("LocalPath(%q) = %q, %v; want %q", tc.host, local, ok, tc.local)
}
host, ok := HostPath(root, "/srv/repo", tc.local)
if !ok || host != tc.host {
t.Errorf("HostPath(%q) = %q, %v; want %q", tc.local, host, ok, tc.host)
}
}
if local, ok := LocalPath(Root(anchor, "c1", "/"), "/", "/etc/hosts"); !ok || local != filepath.Join(Root(anchor, "c1", "/"), "etc", "hosts") {
t.Errorf("LocalPath below the host's root = %q, %v", local, ok)
}
if _, ok := LocalPath(root, "/srv/repo", "relative/path"); ok {
t.Error("mapped a relative host path")
}
for _, local := range []string{filepath.Dir(root), filepath.Join(filepath.Dir(root), "other"), root + "x"} {
if host, ok := HostPath(root, "/srv/repo", local); ok {
t.Errorf("HostPath(%q) = %q; want no host path", local, host)
}
}
}
1 change: 1 addition & 0 deletions desktop/internal/workbench/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ type Kernel interface {
RemoteStat(ctx context.Context, sessionID, path string) (core.RemoteFileMetadata, error)
RemoteReadFile(ctx context.Context, sessionID, path string) ([]byte, core.RemoteFileVersion, error)
RemoteWriteFile(ctx context.Context, sessionID, path string, data []byte, expected *core.RemoteFileVersion) (core.RemoteFileVersion, bool, error)
RemoteCreateDirectory(ctx context.Context, sessionID, path string) error
RemoteServerState(ctx context.Context, sessionID string) error
SearchRemoteConnections(ctx context.Context, document *string, query string) (core.RemoteConnectionsState, error)
RemoteParseAddress(ctx context.Context, text string) (core.RemoteAddress, error)
Expand Down
17 changes: 15 additions & 2 deletions desktop/internal/workbench/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,12 @@ func (b *buildState) active() bool { return b.operation != nil || len(b.running)
// runs; otherwise it stops the one that runs and starts the new one once it ended, so the two
// never share a port or files. A start that is asked again while one waits replaces it.
//
// In safe mode the user is first asked to trust the project, since a run runs its code.
// In safe mode the user is first asked to trust the project, since a run runs its code. A
// remote workspace's project runs on its host, which a run does not reach yet.
func (a *App) startRun(p *project, key string, start func(*App, *project)) {
if a.refuseRemote(p, "run_start") {
return
}
if p.safeMode() {
a.askToTrustProject(p, func() { a.startRun(p, key, start) })
return
Expand Down Expand Up @@ -778,6 +782,11 @@ func (a *App) loadBuildSystems(p *project) {
if p == nil || p.build.loading {
return
}
if p.remote != nil {
// log-exempt: 远程工作区的构建工具要在主机上运行,现在还不支持:本机的占位文件夹里没有项目,不探测构建系统;
// 构建、运行和测试窗口显示只在本机可用的说明,按下运行时 refuseRemote 记录并提示。
return
}
if a.plugins == nil || a.plugins.Backend == nil {
p.build.loaded = true
p.build.failed = a.text.Build.PluginsUnavailable
Expand Down Expand Up @@ -1330,7 +1339,11 @@ func buildTaskSelection(instance *buildInstance, task *pluginservice.BuildTaskDe
// so a tool call can report them, and the Build window keeps its notification and ignores them.
func (a *App) runBuildTask(p *project, projectID, targetID, taskID string, userArguments []string) (*buildOperation, error) {
// A task runs the build tool on the project: in safe mode the user is asked to trust it
// first, and the task starts once trusted. The caller, such as the IDE API, is refused.
// first, and the task starts once trusted. The caller, such as the IDE API, is refused. A
// remote workspace's build tool runs on its host, which a task does not reach yet.
if a.refuseRemote(p, "build_task") {
return nil, refuseBuild(refusalNotReady, "the workspace is remote")
}
if p.safeMode() {
a.askToTrustProject(p, func() { _, _ = a.runBuildTask(p, projectID, targetID, taskID, userArguments) })
return nil, refuseBuild(refusalNotReady, "the workspace is not trusted")
Expand Down
2 changes: 2 additions & 0 deletions desktop/internal/workbench/commit_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func (a *App) drawCommitStatus(c ui.C, p *project) bool {
switch {
case p.git.safeMode:
a.safeModePanel(c, p, i18n.Shared(a.language(), "desktop.trust.gitSafeMode"))
case p.git.remoteAbove:
a.gitEmpty(c, i18n.Shared(a.language(), "desktop.remote.workspace.gitAbove"))
case !p.git.loaded:
a.gitEmpty(c, g.Loading)
case p.git.err != nil:
Expand Down
15 changes: 13 additions & 2 deletions desktop/internal/workbench/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ type gitState struct {
// safeMode is set, with loaded, when the status was not read because the project is in safe
// mode: Core runs no Git there (#2208), and the Git views offer to trust the project.
safeMode bool
status core.GitStatus
// remoteAbove is set, with loaded, when a remote workspace's folder lies inside a repository
// that starts above it on the host, which a remote workspace does not show yet
// (remoteRepositoryAbove).
remoteAbove bool
status core.GitStatus
err error
// tree is the same answer indexed for the project tree, which colors the rows of the
// changed paths. It is rebuilt whenever the status is replaced.
Expand Down Expand Up @@ -58,10 +62,13 @@ func (a *App) readGit(p *project, policy func(context.Context) context.Context)
return func(a *App) {
wasRepo := p.git.isRepository()
g := gitState{loaded: true, status: status, err: err}
if core.IsGitUntrustedWorkspace(err) {
switch {
case core.IsGitUntrustedWorkspace(err):
// The trust was withdrawn while the read was on its way: Core ran no Git, and the
// views show safe mode rather than a failure.
g = gitState{loaded: true, safeMode: true}
case err == nil && remoteRepositoryAbove(p, status):
g = gitState{loaded: true, remoteAbove: true}
}
if !a.setGit(p, g, generation) {
return
Expand All @@ -70,6 +77,10 @@ func (a *App) readGit(p *project, policy func(context.Context) context.Context)
a.backgroundFailed("git_status_safe_mode", err)
return
}
if g.remoteAbove {
// log-exempt: 预期的缺失:远程工作区的文件夹在更上层的仓库里,Git 视图说明原因,不是失败。
return
}
if err != nil {
a.operationFailed("git_status", err)
a.notifyError(a.text.Git.Failed(err.Error()))
Expand Down
48 changes: 35 additions & 13 deletions desktop/internal/workbench/git_exclude.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func excludeLocallyPattern(rel string) string {
}

// excludeLocally adds the change's pattern to the repository's info/exclude and reads the status
// again.
// again. A remote workspace's repository is on its host, where the file is written
// (remoteDocuments.appendInfoExclude).
func (a *App) excludeLocally(p *project, change core.GitChange) {
kernel, ok := a.kernel.(gitObserveKernel)
if !ok {
Expand All @@ -46,24 +47,50 @@ func (a *App) excludeLocally(p *project, change core.GitChange) {
a.performed("git.excludeLocally", surfaceContextMenu)
root := p.git.repositoryRoot(p.root)
pattern := excludeLocallyPattern(change.Path)
host := a.remoteDocuments(p)
a.background(p, gitTimeout, func(ctx context.Context) func(*App) {
watch, found, err := kernel.GitWatchContextOf(ctx, root)
if err == nil && !found {
err = errors.New("not a Git repository")
}
if err == nil {
if err == nil && host != nil {
err = host.appendInfoExclude(ctx, watch.GitCommonDirectory, pattern)
} else if err == nil {
err = appendInfoExclude(watch.GitCommonDirectory, pattern)
}
return func(a *App) {
if err != nil {
a.notifyError(i18n.SharedFormat(a.language(), "desktop.git.excludeFailed", map[string]string{"reason": reviewReason(err)}))
a.operationFailed("git_exclude", err)
a.notifyError(i18n.SharedFormat(a.language(), "desktop.git.excludeFailed", map[string]string{"reason": a.excludeFailureReason(err)}))
a.operationFailed("git_exclude", p.logError(documentLogError(err)))
}
a.reloadGit(p)
}
})
}

// excludeFailureReason is the words for a failed exclusion: a remote workspace's own reasons, such
// as a lost connection, or the error itself.
func (a *App) excludeFailureReason(err error) string {
if reason, ok := a.remoteDocumentReason(err); ok {
return reason
}
return reviewReason(err)
}

// infoExcludeAddition is the text that adds pattern to an info/exclude that holds existing; ok is
// false when the file already has that line.
func infoExcludeAddition(existing []byte, pattern string) (addition string, ok bool) {
for _, line := range strings.Split(string(existing), "\n") {
if line == pattern {
return "", false
}
}
if len(existing) > 0 && !strings.HasSuffix(string(existing), "\n") {
return "\n" + pattern + "\n", true
}
return pattern + "\n", true
}

// appendInfoExclude appends pattern to commonDir/info/exclude unless the file already has that
// line. The file is created with its directory when missing.
func appendInfoExclude(commonDir, pattern string) error {
Expand All @@ -76,20 +103,15 @@ func appendInfoExclude(commonDir, pattern string) error {
if err != nil && !os.IsNotExist(err) {
return err
}
for _, line := range strings.Split(string(existing), "\n") {
if line == pattern {
return nil
}
}
prefix := ""
if len(existing) > 0 && !strings.HasSuffix(string(existing), "\n") {
prefix = "\n"
addition, ok := infoExcludeAddition(existing, pattern)
if !ok {
return nil
}
out, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644)
if err != nil {
return err
}
if _, err := out.WriteString(prefix + pattern + "\n"); err != nil {
if _, err := out.WriteString(addition); err != nil {
out.Close()
return err
}
Expand Down
Loading
Loading