Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: include files not created with new projects (#2463)
Kyle Mendellfcbbed5dd091
diff --git a/backend/internal/services/project_service.go b/backend/internal/services/project_service.goindex ede34e8a5..2f3123831 100644--- a/backend/internal/services/project_service.go+++ b/backend/internal/services/project_service.go@@ -2618,8 +2618,12 @@ func (s *ProjectService) validateComposeContentForUpdate(ctx context.Context, prEnvironment: composetypes.Mapping(fullEnvMap),}+ missingIncludeLoader := &missingIncludeStubResourceLoaderInternal{projectPath: projectPath}+ defer missingIncludeLoader.cleanupInternal()+err = withTransientValidationEnvFile(projectPath, effectiveEnvContent, func() error {_, loadErr := loader.LoadWithContext(ctx, cfg, func(opts *loader.Options) {+ opts.ResourceLoaders = append([]loader.ResourceLoader{missingIncludeLoader}, opts.ResourceLoaders...)if validationProjectName != "" {opts.SetProjectName(validationProjectName, true)}@@ -2630,6 +2634,79 @@ func (s *ProjectService) validateComposeContentForUpdate(ctx context.Context, prreturn err}+type missingIncludeStubResourceLoaderInternal struct {+ projectPath string+ tempDir string+ stubs map[string]string+}++func (l *missingIncludeStubResourceLoaderInternal) Accept(path string) bool {+ _, ok := l.resolveMissingIncludeInternal(path)+ return ok+}++func (l *missingIncludeStubResourceLoaderInternal) Load(_ context.Context, path string) (string, error) {+ validatedPath, ok := l.resolveMissingIncludeInternal(path)+ if !ok {+ return "", fmt.Errorf("include file is not eligible for validation stub: %s", path)+ }++ if l.stubs == nil {+ l.stubs = make(map[string]string)+ }+ if stubPath, ok := l.stubs[validatedPath]; ok {+ return stubPath, nil+ }++ if l.tempDir == "" {+ tempDir, err := os.MkdirTemp("", "arcane-compose-include-*")+ if err != nil {+ return "", fmt.Errorf("create validation include temp dir: %w", err)+ }+ l.tempDir = tempDir+ }++ relPath, err := filepath.Rel(l.projectPath, validatedPath)+ if err != nil || strings.HasPrefix(relPath, "..") || filepath.IsAbs(relPath) {+ relPath = filepath.Base(validatedPath)+ }+ stubPath := filepath.Join(l.tempDir, relPath)+ if err := os.MkdirAll(filepath.Dir(stubPath), 0o755); err != nil {+ return "", fmt.Errorf("create validation include directory: %w", err)+ }+ if err := os.WriteFile(stubPath, []byte("services: {}\n"), 0o600); err != nil {+ return "", fmt.Errorf("write validation include stub: %w", err)+ }++ l.stubs[validatedPath] = stubPath+ return stubPath, nil+}++func (l *missingIncludeStubResourceLoaderInternal) Dir(path string) string {+ return filepath.Dir(path)+}++func (l *missingIncludeStubResourceLoaderInternal) resolveMissingIncludeInternal(path string) (string, bool) {+ validatedPath, err := projects.ValidateIncludePathForWrite(l.projectPath, path)+ if err != nil {+ return "", false+ }++ if _, err := os.Stat(validatedPath); err == nil {+ return "", false+ } else if !errors.Is(err, os.ErrNotExist) {+ return "", false+ }++ return validatedPath, true+}++func (l *missingIncludeStubResourceLoaderInternal) cleanupInternal() {+ if l.tempDir != "" {+ _ = os.RemoveAll(l.tempDir)+ }+}+func buildComposeValidationEnvironment(projectsDirectory, projectPath string, effectiveEnvContent *string) (projects.EnvMap, error) {// Validation should match project-visible env sources without inheriting the// Arcane process environment, which may contain unrelated secrets.diff --git a/backend/internal/services/project_service_test.go b/backend/internal/services/project_service_test.goindex 055a5beb3..ec8597c89 100644--- a/backend/internal/services/project_service_test.go+++ b/backend/internal/services/project_service_test.go@@ -1093,6 +1093,114 @@ func TestProjectService_UpdateProject_AllowsMissingEnvFileDuringComposeValidatiorequire.NoError(t, statErr)}+func TestProjectService_UpdateProject_AllowsMissingLocalIncludeDuringComposeValidation(t *testing.T) {+ db := setupProjectTestDB(t)+ ctx := context.Background()++ projectsDir := t.TempDir()+ t.Setenv("PROJECTS_DIRECTORY", projectsDir)++ settingsService, err := NewSettingsService(ctx, db)+ require.NoError(t, err)++ eventService := NewEventService(db, nil, nil)+ svc := NewProjectService(db, settingsService, eventService, nil, nil, nil, config.Load())++ dirName := "include-new"+ projectPath := filepath.Join(projectsDir, dirName)+ require.NoError(t, os.MkdirAll(projectPath, 0o755))++ project := &models.Project{+ BaseModel: models.BaseModel{ID: "proj-missing-include"},+ Name: "include-new",+ DirName: &dirName,+ Path: projectPath,+ Status: models.ProjectStatusStopped,+ }+ require.NoError(t, db.Create(project).Error)++ compose := `include:+ - metadata.yaml+services:+ app:+ image: nginx:alpine+`++ updated, err := svc.UpdateProject(ctx, project.ID, nil, ptr(compose), nil, models.User{+ BaseModel: models.BaseModel{ID: "u1"},+ Username: "tester",+ })+ require.NoError(t, err)+ require.NotNil(t, updated)++ includePath := filepath.Join(projectPath, "metadata.yaml")+ assert.NoFileExists(t, includePath)++ details, err := svc.GetProjectDetails(ctx, project.ID)+ require.NoError(t, err)+ require.Len(t, details.IncludeFiles, 1)+ assert.Equal(t, "metadata.yaml", details.IncludeFiles[0].RelativePath)++ includeFile, err := svc.GetProjectFileContent(ctx, project.ID, "metadata.yaml")+ require.NoError(t, err)+ assert.Equal(t, "metadata.yaml", includeFile.RelativePath)+ assert.Contains(t, includeFile.Content, "This file will be created when you save changes")++ includeContent := "services: {}\n"+ require.NoError(t, svc.UpdateProjectIncludeFile(ctx, project.ID, "metadata.yaml", includeContent, models.User{+ BaseModel: models.BaseModel{ID: "u1"},+ Username: "tester",+ }))++ writtenContent, err := os.ReadFile(includePath)+ require.NoError(t, err)+ assert.Equal(t, includeContent, string(writtenContent))+}++func TestProjectService_UpdateProject_RejectsMissingExternalIncludeDuringComposeValidation(t *testing.T) {+ db := setupProjectTestDB(t)+ ctx := context.Background()++ projectsDir := t.TempDir()+ t.Setenv("PROJECTS_DIRECTORY", projectsDir)++ settingsService, err := NewSettingsService(ctx, db)+ require.NoError(t, err)++ eventService := NewEventService(db, nil, nil)+ svc := NewProjectService(db, settingsService, eventService, nil, nil, nil, config.Load())++ dirName := "include-external"+ projectPath := filepath.Join(projectsDir, dirName)+ require.NoError(t, os.MkdirAll(projectPath, 0o755))++ project := &models.Project{+ BaseModel: models.BaseModel{ID: "proj-external-include"},+ Name: "include-external",+ DirName: &dirName,+ Path: projectPath,+ Status: models.ProjectStatusStopped,+ }+ require.NoError(t, db.Create(project).Error)++ compose := `include:+ - ../metadata.yaml+services:+ app:+ image: nginx:alpine+`++ updated, err := svc.UpdateProject(ctx, project.ID, nil, ptr(compose), nil, models.User{+ BaseModel: models.BaseModel{ID: "u1"},+ Username: "tester",+ })+ require.Error(t, err)+ assert.Nil(t, updated)+ assert.Contains(t, err.Error(), "invalid compose file")+ assert.NoFileExists(t, filepath.Join(projectsDir, "metadata.yaml"))+ assert.NoFileExists(t, filepath.Join(projectPath, "compose.yaml"))+}+func TestProjectService_UpdateProject_UsesExistingEnvFileDuringComposeValidation(t *testing.T) {db := setupProjectTestDB(t)ctx := context.Background()