Skip to content

Commit 6a1ba56

Browse files
authored
Retain current working directory between steeps (#267)
Make it so that the CWD is preserved between runs similar to how it is for ENV variables
1 parent 0a125a5 commit 6a1ba56

File tree

7 files changed

+106
-35
lines changed

7 files changed

+106
-35
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.md text eol=lf
2+
*.sh text eol=lf

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Python
22
__pycache__
33

4-
#VS Code
5-
.vscode
6-
74
# Ignore all binaries.
85
bin/
96

.vscode/launch.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch file",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "debug",
12+
"cwd": "${workspaceFolder}",
13+
"program": "${workspaceFolder}/cmd/ie/ie.go",
14+
"console": "integratedTerminal",
15+
"args": [
16+
"interactive",
17+
"--working-directory",
18+
"${workspaceFolder}",
19+
"${workspaceFolder}/${input:file}"
20+
]
21+
},
22+
],
23+
"inputs": [
24+
{
25+
"id": "file",
26+
"type": "promptString",
27+
"description": "Enter the path to the file to process",
28+
"default": "tutorial.md"
29+
}
30+
]
31+
}

SUPPORT.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
# TODO: The maintainer of this repo has not yet edited this file
2-
3-
**REPO OWNER**: Do you want Customer Service & Support (CSS) support for this product/project?
4-
5-
- **No CSS support:** Fill out this template with information about how to file issues and get help.
6-
- **Yes CSS support:** Fill out an intake form at [aka.ms/onboardsupport](https://aka.ms/onboardsupport). CSS will work with/help you to determine next steps.
7-
- **Not sure?** Fill out an intake as though the answer were "Yes". CSS will help you decide.
8-
9-
*Then remove this first heading from this SUPPORT.MD file before publishing your repo.*
10-
11-
# Support
12-
13-
## How to file issues and get help
14-
15-
This project uses GitHub Issues to track bugs and feature requests. Please search the existing
16-
issues before filing new issues to avoid duplicates. For new issues, file your bug or
17-
feature request as a new Issue.
18-
19-
For help and questions about using this project, please **REPO MAINTAINER: INSERT INSTRUCTIONS HERE
20-
FOR HOW TO ENGAGE REPO OWNERS OR COMMUNITY FOR HELP. COULD BE A STACK OVERFLOW TAG OR OTHER
21-
CHANNEL. WHERE WILL YOU HELP PEOPLE?**.
22-
23-
## Microsoft Support Policy
24-
25-
Support for this **PROJECT or PRODUCT** is limited to the resources listed above.
1+
# TODO: The maintainer of this repo has not yet edited this file
2+
3+
**REPO OWNER**: Do you want Customer Service & Support (CSS) support for this product/project?
4+
5+
- **No CSS support:** Fill out this template with information about how to file issues and get help.
6+
- **Yes CSS support:** Fill out an intake form at [aka.ms/onboardsupport](https://aka.ms/onboardsupport). CSS will work with/help you to determine next steps.
7+
- **Not sure?** Fill out an intake as though the answer were "Yes". CSS will help you decide.
8+
9+
*Then remove this first heading from this SUPPORT.MD file before publishing your repo.*
10+
11+
# Support
12+
13+
## How to file issues and get help
14+
15+
This project uses GitHub Issues to track bugs and feature requests. Please search the existing
16+
issues before filing new issues to avoid duplicates. For new issues, file your bug or
17+
feature request as a new Issue.
18+
19+
For help and questions about using this project, please **REPO MAINTAINER: INSERT INSTRUCTIONS HERE
20+
FOR HOW TO ENGAGE REPO OWNERS OR COMMUNITY FOR HELP. COULD BE A STACK OVERFLOW TAG OR OTHER
21+
CHANNEL. WHERE WILL YOU HELP PEOPLE?**.
22+
23+
## Microsoft Support Policy
24+
25+
Support for this **PROJECT or PRODUCT** is limited to the resources listed above.

internal/engine/execution.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,18 @@ func (e *Engine) ExecuteAndRenderSteps(steps []common.Step, env map[string]strin
316316
return err
317317
}
318318

319+
logging.GlobalLogger.Info(
320+
"Cleaning working directory file located at /tmp/working-dir",
321+
)
322+
err = lib.DeleteWorkingDirectoryStateFile(lib.DefaultWorkingDirectoryStateFile)
323+
if err != nil {
324+
logging.GlobalLogger.Errorf("Error cleaning working directory: %s", err.Error())
325+
return err
326+
}
327+
319328
default:
320329
lib.DeleteEnvironmentStateFile(lib.DefaultEnvironmentStateFile)
330+
lib.DeleteWorkingDirectoryStateFile(lib.DefaultWorkingDirectoryStateFile)
321331
}
322332

323333
return nil

internal/lib/env.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ func GetEnvironmentVariables() map[string]string {
2323
return envMap
2424
}
2525

26-
// Location where the environment state from commands are to be captured
27-
// and sent to for being able to share state across commands.
26+
// Location where the environment and working directory state from commands are
27+
// to be captured and sent to for being able to share state across commands.
2828
var DefaultEnvironmentStateFile = "/tmp/env-vars"
29+
var DefaultWorkingDirectoryStateFile = "/tmp/working-dir"
2930

3031
// Loads a file that contains environment variables
3132
func LoadEnvironmentStateFile(path string) (map[string]string, error) {
@@ -97,3 +98,23 @@ func filterInvalidKeys(envMap map[string]string) map[string]string {
9798
func DeleteEnvironmentStateFile(path string) error {
9899
return os.Remove(path)
99100
}
101+
102+
// Loads a file that contains the working directory
103+
func LoadWorkingDirectoryStateFile(path string) (string, error) {
104+
if !fs.FileExists(path) {
105+
return "", fmt.Errorf("working directory file '%s' does not exist", path)
106+
}
107+
108+
content, err := os.ReadFile(path)
109+
if err != nil {
110+
return "", fmt.Errorf("failed to read working directory file '%s': %w", path, err)
111+
}
112+
113+
workingDir := strings.TrimSpace(string(content))
114+
return workingDir, nil
115+
}
116+
117+
// Deletes the stored working directory file.
118+
func DeleteWorkingDirectoryStateFile(path string) error {
119+
return os.Remove(path)
120+
}

internal/shells/bash.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func executeBashCommandImpl(
5959
command,
6060
"IE_LAST_COMMAND_EXIT_CODE=\"$?\"",
6161
"env > " + lib.DefaultEnvironmentStateFile,
62+
"pwd > " + lib.DefaultWorkingDirectoryStateFile,
6263
"exit $IE_LAST_COMMAND_EXIT_CODE",
6364
}
6465

@@ -82,11 +83,13 @@ func executeBashCommandImpl(
8283
commandToExecute.Env = os.Environ()
8384
}
8485

85-
// Sharing environment variable state between isolated shell executions is a
86-
// bit tough, but how we handle it is by storing the environment variables
87-
// after a command is executed within a file and then loading that file
88-
// before executing the next command. This allows us to share state between
89-
// isolated command calls.
86+
// Sharing environment variables and the working directory between isolated
87+
// shell executions is a bit tough, but how we handle it is by storing the
88+
// environment variables after a command is executed within a file and then
89+
// loading that file before executing the next command. This allows us to
90+
// share state between isolated command calls.
91+
92+
// Restore env variables
9093
envFromPreviousStep, err := lib.LoadEnvironmentStateFile(lib.DefaultEnvironmentStateFile)
9194
if err == nil {
9295
merged := lib.MergeMaps(config.EnvironmentVariables, envFromPreviousStep)
@@ -98,6 +101,13 @@ func executeBashCommandImpl(
98101
commandToExecute.Env = append(commandToExecute.Env, fmt.Sprintf("%s=%s", k, v))
99102
}
100103
}
104+
// Restore working directory
105+
workingDirFromPreviousStep, err := lib.LoadWorkingDirectoryStateFile(lib.DefaultWorkingDirectoryStateFile)
106+
if err == nil {
107+
commandToExecute.Dir = workingDirFromPreviousStep
108+
} else {
109+
commandToExecute.Dir = ""
110+
}
101111

102112
if config.WriteToHistory {
103113

0 commit comments

Comments
 (0)