-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathworkdir.go
More file actions
56 lines (46 loc) · 1.74 KB
/
workdir.go
File metadata and controls
56 lines (46 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package parse
import (
"strings"
"github.com/moby/buildkit/frontend/dockerfile/parser"
)
func parseWorkdir(node *parser.Node) []Rule {
if node.Next == nil {
return []Rule{invalidInstructionRule(node, "WORKDIR requires exactly one argument")}
}
workdirValue := node.Next
var workdirRules []Rule
if !checkWorkdirAbsolute(workdirValue.Value) {
workdirRules = append(workdirRules, NewWarningRule(node, "WorkdirRelativePath",
"WORKDIR uses a relative path. Consider using an absolute path (starting with /) to avoid issues when the base image's working directory changes.",
"https://docs.docker.com/reference/build-checks/workdir-relative-path/"))
}
return workdirRules
}
// checkWorkdirAbsolute checks if a WORKDIR path is absolute.
// Returns true if the path is absolute, false if it's relative.
// Absolute paths include:
// - Unix absolute paths (starting with /)
// - Windows absolute paths (e.g., C:, C:\, C:/)
// - Variable references (e.g., $HOME, ${WORKDIR}) - considered "absolute enough"
func checkWorkdirAbsolute(workdirPath string) bool {
// Unix absolute path
if strings.HasPrefix(workdirPath, "/") {
return true
}
// Windows absolute path (C:, C:\, C:/)
// Check for drive letter followed by colon
if len(workdirPath) >= 2 && workdirPath[1] == ':' {
// First character should be a letter (drive letter)
firstChar := workdirPath[0]
if (firstChar >= 'A' && firstChar <= 'Z') || (firstChar >= 'a' && firstChar <= 'z') {
return true
}
}
// Variable reference (considered "absolute enough")
// Variables like $HOME, ${WORKDIR}, $APP_HOME are treated as absolute
// since they will be resolved at build time and we can't know if they're absolute
if strings.HasPrefix(workdirPath, "$") {
return true
}
return false
}