Skip to content

Commit 8ad9b2e

Browse files
authored
Don't generate .dockerignore during debugging (#1336)
* Don't generate .dockerignore during debugging * Call debugger on composeUp errors (#1335) * Call debugger on composeUp errors * Don't generate .dockerignore during debugging * Address PR comment
1 parent 5dee0f1 commit 8ad9b2e

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

src/pkg/cli/compose/context.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,16 @@ func tryReadIgnoreFile(cwd, ignorefile string) io.ReadCloser {
277277
// writeDefaultIgnoreFile writes a default
278278
// .dockerignore file to the specified directory.
279279
// Returns the filename of the written file and an error.
280-
func writeDefaultIgnoreFile(cwd string) (string, error) {
281-
path := filepath.Join(cwd, dotdockerignore)
280+
func writeDefaultIgnoreFile(cwd string, dockerignore string) (string, error) {
281+
path := filepath.Join(cwd, dockerignore)
282282
term.Debug("Writing .dockerignore file to", path)
283283

284284
err := os.WriteFile(path, []byte(defaultDockerIgnore), 0644)
285285
if err != nil {
286286
return "", fmt.Errorf("failed to write default .dockerignore file: %w", err)
287287
}
288288

289-
return dotdockerignore, nil
289+
return dockerignore, nil
290290
}
291291

292292
// getDockerIgnorePatterns attempts to read the ignore file
@@ -302,18 +302,9 @@ func getDockerIgnorePatterns(root, dockerfile string) ([]string, string, error)
302302
dockerignore = dotdockerignore
303303
reader = tryReadIgnoreFile(root, dockerignore)
304304
if reader == nil {
305-
// Generate a default .dockerignore file if none exists
306-
term.Warn("No .dockerignore file found; generating default .dockerignore")
307-
var err error
308-
dockerignore, err = writeDefaultIgnoreFile(root)
309-
if err != nil {
310-
return nil, "", fmt.Errorf("failed to write default .dockerignore file: %w", err)
311-
}
312-
// Try reading the newly created .dockerignore file
313-
reader = tryReadIgnoreFile(root, dockerignore)
314-
if reader == nil {
315-
return nil, "", fmt.Errorf("failed to read default .dockerignore file: %s at the path: %s", dockerignore, root)
316-
}
305+
// No .dockerignore file found; read from defaults
306+
dockerignore = ""
307+
reader = io.NopCloser(strings.NewReader(defaultDockerIgnore))
317308
}
318309
}
319310

@@ -328,6 +319,15 @@ func getDockerIgnorePatterns(root, dockerfile string) ([]string, string, error)
328319
}
329320

330321
func WalkContextFolder(root, dockerfile string, fn func(path string, de os.DirEntry, slashPath string) error) error {
322+
return walkContextFolder(root, dockerfile, writeIgnoreFileNo, fn)
323+
}
324+
325+
type writeIgnoreFile bool
326+
327+
const writeIgnoreFileNo writeIgnoreFile = false
328+
const writeIgnoreFileYes writeIgnoreFile = true
329+
330+
func walkContextFolder(root, dockerfile string, writeIgnore writeIgnoreFile, fn func(path string, de os.DirEntry, slashPath string) error) error {
331331
if dockerfile == "" {
332332
dockerfile = "Dockerfile"
333333
} else {
@@ -340,6 +340,16 @@ func WalkContextFolder(root, dockerfile string, fn func(path string, de os.DirEn
340340
return err
341341
}
342342

343+
if dockerignore == "" && writeIgnore {
344+
// Generate a default .dockerignore file if none exists (to be included in the context)
345+
term.Warn("No .dockerignore file found; creating default .dockerignore")
346+
var err error
347+
dockerignore, err = writeDefaultIgnoreFile(root, dotdockerignore)
348+
if err != nil {
349+
return fmt.Errorf("failed to write default .dockerignore file: %w", err)
350+
}
351+
}
352+
343353
pm, err := patternmatcher.New(patterns)
344354
if err != nil {
345355
return err
@@ -363,11 +373,12 @@ func WalkContextFolder(root, dockerfile string, fn func(path string, de os.DirEn
363373

364374
slashPath := filepath.ToSlash(relPath)
365375

366-
if relPath == dockerfile {
376+
switch relPath {
377+
case dockerfile:
367378
// we need the Dockerfile, even if it's in the .dockerignore file
368-
} else if relPath == dockerignore {
379+
case dockerignore:
369380
// we need the .dockerignore file too: it might ignore itself and/or the Dockerfile, but is needed by the builder
370-
} else {
381+
default:
371382
// Ignore files using the dockerignore patternmatcher
372383
ignore, err := pm.MatchesOrParentMatches(slashPath) // always use forward slashes
373384
if err != nil {
@@ -407,7 +418,7 @@ func createArchive(ctx context.Context, root string, dockerfile string, contentT
407418
}
408419

409420
doProgress := term.StdoutCanColor() && term.IsTerminal()
410-
err := WalkContextFolder(root, dockerfile, func(path string, de os.DirEntry, slashPath string) error {
421+
err := walkContextFolder(root, dockerfile, writeIgnoreFileYes, func(path string, de os.DirEntry, slashPath string) error {
411422
if term.DoDebug() {
412423
term.Debug("Adding", slashPath)
413424
} else if doProgress {

src/pkg/cli/compose/context_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestWalkContextFolder(t *testing.T) {
135135
t.Fatalf("WalkContextFolder() failed: %v", err)
136136
}
137137

138-
expected := []string{".dockerignore", "Dockerfile", "altcomp.yaml", "compose.yaml.fixup", "compose.yaml.golden", "compose.yaml.warnings"}
138+
expected := []string{"Dockerfile", "altcomp.yaml", "compose.yaml.fixup", "compose.yaml.golden", "compose.yaml.warnings"}
139139
if !reflect.DeepEqual(files, expected) {
140140
t.Errorf("Expected files: %v, got %v", expected, files)
141141
}
@@ -222,7 +222,7 @@ func TestGetDockerIgnorePatterns(t *testing.T) {
222222
dockerfile: "Dockerfile",
223223
ignoreFileName: "",
224224
ignoreFileContent: defaultDockerIgnore,
225-
expectedFileName: ".dockerignore",
225+
expectedFileName: "",
226226
},
227227
{
228228
name: "No dockerfile, but dockerignore exists",
@@ -236,7 +236,7 @@ func TestGetDockerIgnorePatterns(t *testing.T) {
236236
dockerfile: "",
237237
ignoreFileName: "",
238238
ignoreFileContent: defaultDockerIgnore,
239-
expectedFileName: ".dockerignore",
239+
expectedFileName: "",
240240
},
241241
}
242242

src/pkg/cli/debug.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
const maxFiles = 20
2626

2727
var (
28-
ErrDebugSkipped = errors.New("debug skipped")
29-
3028
errFileLimitReached = errors.New("file limit reached")
3129
patterns = []string{"*.js", "*.ts", "*.py", "*.go", "requirements.txt", "package.json", "go.mod"} // TODO: add patterns for other languages
3230
)
@@ -86,7 +84,7 @@ func interactiveDebug(ctx context.Context, client client.FabricClient, debugConf
8684
return err
8785
} else if !aiDebug {
8886
track.Evt("Debug Prompt Skipped", P("etag", debugConfig.Deployment), P("loadErr", clientErr))
89-
return ErrDebugSkipped
87+
return err
9088
}
9189

9290
track.Evt("Debug Prompt Accepted", P("etag", debugConfig.Deployment), P("loadErr", clientErr))

0 commit comments

Comments
 (0)