Skip to content

Commit 208e7fc

Browse files
author
Binu Jose Philip
authored
style: Add newline between error context string and error (#347)
* Add newline between error context string and error * update scenario snapshot and coding conventions * add one more instance of newline before %w
1 parent 50c27fd commit 208e7fc

57 files changed

Lines changed: 263 additions & 260 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/instructions/go.instructions.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ applyTo: "**/*.go"
2828
- Interface Segregation - Prefer small, specific interfaces over large, general-purpose ones
2929
- Programming for interfaces, not implementations - Use interfaces to define behavior and allow for flexible implementations
3030
- Stick strictly to the existing coding conventions in this repository
31-
- For error handling we like to wrap errors: `return fmt.Errorf("context: %w", err)`. Define global errors where it makes sense: `var ErrName = errors.New("...")`
31+
- For error handling we like to wrap errors: `return fmt.Errorf("context:\n%w", err)`. Define global errors where it makes sense: `var ErrName = errors.New("...")`
32+
- For error messages with context, add a newline after the context message, before the error format specifier. Examples:
33+
- `fmt.Errorf("This is an error context with wrapped error:\n%w")`
34+
- `fmt.Errorf("This is a regular error with context string:\n%v")`
3235
- Follow established Go language practices and conventions:
3336
- Follow Go naming conventions (e.g., CamelCase for exported names)
3437
- Write concise, readable code with appropriate comments
3538
- Use Go idioms and standard library functions where appropriate
3639
- In formatted strings enclose string types in quotes. For that purpose use the `%#q` format verb unless the message already encloses the string in quotes. Examples:
37-
- `return fmt.Errorf("failed to open %#q: %w", filename, err)`
38-
- `return fmt.Errorf("failed to run command 'go %s': %w", strings.Join(args, " "), err)`
40+
- `return fmt.Errorf("failed to open %#q:\n%w", filename, err)`
41+
- `return fmt.Errorf("failed to run command 'go %s':\n%w", strings.Join(args, " "), err)`
3942
- Comments referring to types should encapsulate the type name in square brackets. Example: `// [packagename.MyType] is a custom type`
4043
- Use structured logging with slog
4144
- Ensure code passes golangci-lint checks

internal/app/azldev/app.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func (a *App) Execute(args []string) int {
317317
func (*App) setCmdFactory(envOptions *EnvOptions) error {
318318
cmdFactory, err := DefaultCmdFactory(envOptions.DryRunnable, envOptions.EventListener)
319319
if err != nil {
320-
return fmt.Errorf("error creating command factory: %w", err)
320+
return fmt.Errorf("error creating command factory:\n%w", err)
321321
}
322322

323323
envOptions.Interfaces.CmdFactory = cmdFactory
@@ -334,12 +334,12 @@ func (a *App) reInitLoggingWithLogFile(envOptions *EnvOptions) error {
334334

335335
logger, err := a.initFileLogging(envOptions.Config.Project.LogDir)
336336
if err != nil {
337-
return fmt.Errorf("error re-initializing file logging: %w", err)
337+
return fmt.Errorf("error re-initializing file logging:\n%w", err)
338338
}
339339

340340
err = setEventListener(logger, envOptions)
341341
if err != nil {
342-
return fmt.Errorf("error re-setting event listener: %w", err)
342+
return fmt.Errorf("error re-setting event listener:\n%w", err)
343343
}
344344

345345
return nil
@@ -365,7 +365,7 @@ func (a *App) initializeProjectConfig(envOptions *EnvOptions, earlyTempDirPath s
365365
// Notify the user, but move on.
366366
slog.Info("No Azure Linux project found; some commands will not be available.")
367367
} else if err != nil {
368-
return fmt.Errorf("error loading project configuration: %w", err)
368+
return fmt.Errorf("error loading project configuration:\n%w", err)
369369
}
370370

371371
envOptions.ProjectDir = projectDir
@@ -394,7 +394,7 @@ func (a *App) handlePostInitCallbacks(env *Env) error {
394394

395395
err := a.callPostInitCallbacks(env)
396396
if err != nil {
397-
return fmt.Errorf("error during post-config initialization: %w", err)
397+
return fmt.Errorf("error during post-config initialization:\n%w", err)
398398
}
399399

400400
return nil
@@ -403,7 +403,7 @@ func (a *App) handlePostInitCallbacks(env *Env) error {
403403
func setEventListener(stdioLogger *slog.Logger, envOptions *EnvOptions) error {
404404
eventListener, err := NewEventListener(stdioLogger)
405405
if err != nil {
406-
return fmt.Errorf("error initializing event listener: %w", err)
406+
return fmt.Errorf("error initializing event listener:\n%w", err)
407407
}
408408

409409
envOptions.EventListener = eventListener
@@ -462,7 +462,7 @@ func (a *App) findAndLoadConfig(dryRunnable opctx.DryRunnable, tempDirPath strin
462462
if referenceDir == "" {
463463
referenceDir, err = a.osEnvFactory.OSEnv().Getwd()
464464
if err != nil {
465-
return projectDir, config, fmt.Errorf("failed to get working directory: %w", err)
465+
return projectDir, config, fmt.Errorf("failed to get working directory:\n%w", err)
466466
}
467467
}
468468

@@ -476,7 +476,7 @@ func (a *App) findAndLoadConfig(dryRunnable opctx.DryRunnable, tempDirPath strin
476476
tempDirPath,
477477
)
478478
if err != nil {
479-
return projectDir, config, fmt.Errorf("failed to load project configuration: %w", err)
479+
return projectDir, config, fmt.Errorf("failed to load project configuration:\n%w", err)
480480
}
481481

482482
return projectDir, config, nil
@@ -537,15 +537,15 @@ func (a *App) initFileLogging(logDir string) (*slog.Logger, error) {
537537

538538
err := fileutils.MkdirAll(fs, logDir)
539539
if err != nil {
540-
return nil, fmt.Errorf("failed to ensure log dir %#q exists: %w", logDir, err)
540+
return nil, fmt.Errorf("failed to ensure log dir %#q exists:\n%w", logDir, err)
541541
}
542542

543543
// Create the log file anew.
544544
logFilePath := filepath.Join(logDir, defaultLogFilename)
545545

546546
logFile, err := fs.Create(logFilePath)
547547
if err != nil {
548-
return nil, fmt.Errorf("failed to create log file at %#q: %w", logFilePath, err)
548+
return nil, fmt.Errorf("failed to create log file at %#q:\n%w", logFilePath, err)
549549
}
550550

551551
// We log with the configured verbosity level to

internal/app/azldev/cmds/advanced/mcp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewMCPCmd() *cobra.Command {
2424
RunE: (func(cmd *cobra.Command, args []string) error {
2525
env, err := azldev.GetEnvFromCommand(cmd)
2626
if err != nil {
27-
return fmt.Errorf("failed to get command environment: %w", err)
27+
return fmt.Errorf("failed to get command environment:\n%w", err)
2828
}
2929

3030
return mcp.RunMCPServer(env, cmd)

internal/app/azldev/cmds/advanced/wget.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ func NewWgetCmd() *cobra.Command {
4747
func Download(env *azldev.Env, options *DownloadOptions) error {
4848
httpDownloader, err := downloader.NewHTTPDownloader(env, env, env.FS())
4949
if err != nil {
50-
return fmt.Errorf("failed to create downloader: %w", err)
50+
return fmt.Errorf("failed to create downloader:\n%w", err)
5151
}
5252

5353
err = httpDownloader.Download(env, options.uri, options.outputFilePath)
5454
if err != nil {
55-
return fmt.Errorf("failed to download '%s' to '%s': %w", options.uri, options.outputFilePath, err)
55+
return fmt.Errorf("failed to download '%s' to '%s':\n%w", options.uri, options.outputFilePath, err)
5656
}
5757

5858
return nil

internal/app/azldev/cmds/component/add.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func AddComponentsToConfig(
7878

7979
err = updateConfigFileFromDoc(fs, config.RootConfigFilePath, doc)
8080
if err != nil {
81-
return fmt.Errorf("failed to update project config file %q: %w", config.RootConfigFilePath, err)
81+
return fmt.Errorf("failed to update project config file %q:\n%w", config.RootConfigFilePath, err)
8282
}
8383

8484
return nil
@@ -90,14 +90,14 @@ func AddComponentsToConfig(
9090
func parseConfigFileForEditing(fs opctx.FS, configFilePath string) (*tomledit.Document, error) {
9191
configFileBytes, err := fileutils.ReadFile(fs, configFilePath)
9292
if err != nil {
93-
return nil, fmt.Errorf("failed to read project config file %q: %w", configFilePath, err)
93+
return nil, fmt.Errorf("failed to read project config file %q:\n%w", configFilePath, err)
9494
}
9595

9696
configFileReader := bytes.NewReader(configFileBytes)
9797

9898
doc, err := tomledit.Parse(configFileReader)
9999
if err != nil {
100-
return nil, fmt.Errorf("failed to parse project config file %q: %w", configFilePath, err)
100+
return nil, fmt.Errorf("failed to parse project config file %q:\n%w", configFilePath, err)
101101
}
102102

103103
return doc, nil
@@ -112,18 +112,18 @@ func updateConfigFileFromDoc(fs opctx.FS, configFilePath string, doc *tomledit.D
112112

113113
updateWriter, err := fileutils.NewFileUpdateWriter(fs, configFilePath)
114114
if err != nil {
115-
return fmt.Errorf("failed to create file update writer for %q: %w", configFilePath, err)
115+
return fmt.Errorf("failed to create file update writer for %q:\n%w", configFilePath, err)
116116
}
117117

118118
err = formatter.Format(updateWriter, doc)
119119
if err != nil {
120-
return fmt.Errorf("failed to update project config file %q: %w", configFilePath, err)
120+
return fmt.Errorf("failed to update project config file %q:\n%w", configFilePath, err)
121121
}
122122

123123
// Commit!
124124
err = updateWriter.Commit()
125125
if err != nil {
126-
return fmt.Errorf("failed to commit updates to project config file %q: %w", configFilePath, err)
126+
return fmt.Errorf("failed to commit updates to project config file %q:\n%w", configFilePath, err)
127127
}
128128

129129
return nil

internal/app/azldev/cmds/component/build.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func SelectAndBuildComponents(env *azldev.Env, options *ComponentBuildOptions,
8686

8787
comps, err := resolver.FindComponents(&options.ComponentFilter)
8888
if err != nil {
89-
return nil, fmt.Errorf("failed to resolve components: %w", err)
89+
return nil, fmt.Errorf("failed to resolve components:\n%w", err)
9090
}
9191

9292
if comps.Len() == 0 {
@@ -112,23 +112,23 @@ func buildComponents(
112112

113113
workDirFactory, err := workdir.NewFactory(env.FS(), env.WorkDir(), env.ConstructionTime())
114114
if err != nil {
115-
return nil, fmt.Errorf("failed to create work dir factory: %w", err)
115+
return nil, fmt.Errorf("failed to create work dir factory:\n%w", err)
116116
}
117117

118118
orchestrator, err := orchestrator.NewOrchestrator(env.FS(),
119119
orchestrator.WithBuildDir(env.WorkDir()),
120120
orchestrator.WithOutputDir(env.OutputDir()),
121121
)
122122
if err != nil {
123-
return nil, fmt.Errorf("failed to create orchestrator: %w", err)
123+
return nil, fmt.Errorf("failed to create orchestrator:\n%w", err)
124124
}
125125

126126
results := make([]ComponentBuildResults, 0, components.Len())
127127

128128
for _, component := range components.Components() {
129129
componentResults, buildErr := BuildComponent(env, orchestrator, component, workDirFactory, options)
130130
if buildErr != nil {
131-
buildErr = fmt.Errorf("failed to build %q: %w", component.GetName(), buildErr)
131+
buildErr = fmt.Errorf("failed to build %q:\n%w", component.GetName(), buildErr)
132132
}
133133

134134
err = errors.Join(err, buildErr)
@@ -156,7 +156,7 @@ func BuildComponent(
156156
if component.GetConfig().Spec.SourceType == projectconfig.SpecSourceTypeUpstream {
157157
rpmContentsProvider, err = buildRPMContentsProvider(env, component)
158158
if err != nil {
159-
return ComponentBuildResults{}, fmt.Errorf("failed to create RPM contents provider: %w", err)
159+
return ComponentBuildResults{}, fmt.Errorf("failed to create RPM contents provider:\n%w", err)
160160
}
161161
}
162162

@@ -165,7 +165,7 @@ func BuildComponent(
165165
buildEnv, err = workdir.MkComponentBuildEnvironment(env, workDirFactory, component.GetConfig(), "build")
166166
if err != nil {
167167
return ComponentBuildResults{},
168-
fmt.Errorf("failed to create build environment for component %q: %w", component.GetName(), err)
168+
fmt.Errorf("failed to create build environment for component %q:\n%w", component.GetName(), err)
169169
}
170170

171171
// Clean up the build environment before we return (unless we were asked not to do so).
@@ -201,7 +201,7 @@ func buildComponentUsingBuilder(
201201

202202
srpmPath, err := buildSRPM(env.Context(), builder, component, orchestrator)
203203
if err != nil {
204-
return results, fmt.Errorf("failed to build SRPM for %q: %w", component.GetName(), err)
204+
return results, fmt.Errorf("failed to build SRPM for %q:\n%w", component.GetName(), err)
205205
}
206206

207207
results.SRPMPaths = []string{srpmPath}
@@ -213,7 +213,7 @@ func buildComponentUsingBuilder(
213213

214214
rpmPaths, err := buildRPMs(env.Context(), builder, component, srpmPath, runChecks, orchestrator)
215215
if err != nil {
216-
return results, fmt.Errorf("failed to build RPMs for %q: %w", component.GetName(), err)
216+
return results, fmt.Errorf("failed to build RPMs for %q:\n%w", component.GetName(), err)
217217
}
218218

219219
results.RPMPaths = rpmPaths
@@ -229,27 +229,27 @@ func buildRPMs(
229229
// and move creation of subtasks into the orchestrator and task factories.
230230
binaryTaskBuilder, err := NewBinaryPackageTaskBuilder(builder, component, srpmPath, runChecks)
231231
if err != nil {
232-
return nil, fmt.Errorf("failed to create binary package task builder: %w", err)
232+
return nil, fmt.Errorf("failed to create binary package task builder:\n%w", err)
233233
}
234234

235235
err = orchestrator.AssignTaskServices(binaryTaskBuilder)
236236
if err != nil {
237-
return nil, fmt.Errorf("failed to assign task services for binary package task: %w", err)
237+
return nil, fmt.Errorf("failed to assign task services for binary package task:\n%w", err)
238238
}
239239

240240
binaryTask, err := binaryTaskBuilder.Build()
241241
if err != nil {
242-
return nil, fmt.Errorf("failed to build binary package task: %w", err)
242+
return nil, fmt.Errorf("failed to build binary package task:\n%w", err)
243243
}
244244

245245
rawBinaryArtifacts, err := orchestrator.RunTask(ctx, binaryTask)
246246
if err != nil {
247-
return nil, fmt.Errorf("failed to run binary package task: %w", err)
247+
return nil, fmt.Errorf("failed to run binary package task:\n%w", err)
248248
}
249249

250250
typedBinaryArtifacts, err := artifacts.Cast[*FileArtifact](rawBinaryArtifacts...)
251251
if err != nil {
252-
return nil, fmt.Errorf("failed to cast binary artifacts: %w", err)
252+
return nil, fmt.Errorf("failed to cast binary artifacts:\n%w", err)
253253
}
254254

255255
// Extract RPM paths from artifacts
@@ -266,22 +266,22 @@ func buildSRPM(
266266
// and move creation of subtasks into the orchestrator and task factories.
267267
sourceTaskBuilder, err := NewSourcesTaskBuilder(builder, component)
268268
if err != nil {
269-
return "", fmt.Errorf("failed to create sources task builder: %w", err)
269+
return "", fmt.Errorf("failed to create sources task builder:\n%w", err)
270270
}
271271

272272
err = orchestrator.AssignTaskServices(sourceTaskBuilder)
273273
if err != nil {
274-
return "", fmt.Errorf("failed to assign task services for sources task: %w", err)
274+
return "", fmt.Errorf("failed to assign task services for sources task:\n%w", err)
275275
}
276276

277277
sourcesTask, err := sourceTaskBuilder.Build()
278278
if err != nil {
279-
return "", fmt.Errorf("failed to build sources task: %w", err)
279+
return "", fmt.Errorf("failed to build sources task:\n%w", err)
280280
}
281281

282282
rawArtifacts, err := orchestrator.RunTask(ctx, sourcesTask)
283283
if err != nil {
284-
return "", fmt.Errorf("failed to run sources task: %w", err)
284+
return "", fmt.Errorf("failed to run sources task:\n%w", err)
285285
}
286286

287287
if len(rawArtifacts) != 1 {
@@ -291,7 +291,7 @@ func buildSRPM(
291291

292292
typedArtifacts, err := artifacts.Cast[*FileArtifact](rawArtifacts...)
293293
if err != nil {
294-
return "", fmt.Errorf("failed to cast artifact: %w", err)
294+
return "", fmt.Errorf("failed to cast artifact:\n%w", err)
295295
}
296296

297297
return typedArtifacts[0].Path, nil

internal/app/azldev/cmds/component/buildtasks.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ func newSourcesTask(
5959

6060
taskID, err := orchestration.NewTaskID[sourcesTask](component.GetName())
6161
if err != nil {
62-
return nil, fmt.Errorf("failed to create task ID for component %q: %w", component.GetName(), err)
62+
return nil, fmt.Errorf("failed to create task ID for component %q:\n%w", component.GetName(), err)
6363
}
6464

6565
baseTask, err := orchestration.NewBaseTask(taskID)
6666
if err != nil {
67-
return nil, fmt.Errorf("failed to create base task for component %q: %w", component.GetName(), err)
67+
return nil, fmt.Errorf("failed to create base task for component %q:\n%w", component.GetName(), err)
6868
}
6969

7070
// Create a new task for the component.
@@ -82,7 +82,7 @@ func (t *sourcesTask) Run(ctx context.Context) ([]artifacts.Artifact, error) {
8282

8383
outputSourcePackagePath, err := t.componentBuilder.BuildSourcePackage(ctx, t.component, outputDir)
8484
if err != nil {
85-
return nil, fmt.Errorf("failed to build SRPM for %#q: %w", t.component.GetName(), err)
85+
return nil, fmt.Errorf("failed to build SRPM for %#q:\n%w", t.component.GetName(), err)
8686
}
8787

8888
if outputSourcePackagePath == "" {
@@ -107,7 +107,7 @@ func NewSourcesTaskBuilder(
107107
) (*sourcesTaskBuilder, error) {
108108
builtTask, err := newSourcesTask(componentBuilder, component)
109109
if err != nil {
110-
return nil, fmt.Errorf("failed to create sources task: %w", err)
110+
return nil, fmt.Errorf("failed to create sources task:\n%w", err)
111111
}
112112

113113
return &sourcesTaskBuilder{
@@ -164,12 +164,12 @@ func newBinaryPackageTask(
164164

165165
taskID, err := orchestration.NewTaskID[binaryPackageTask](component.GetName())
166166
if err != nil {
167-
return nil, fmt.Errorf("failed to create task ID for component %q: %w", component.GetName(), err)
167+
return nil, fmt.Errorf("failed to create task ID for component %q:\n%w", component.GetName(), err)
168168
}
169169

170170
baseTask, err := orchestration.NewBaseTask(taskID)
171171
if err != nil {
172-
return nil, fmt.Errorf("failed to create base task for component %q: %w", component.GetName(), err)
172+
return nil, fmt.Errorf("failed to create base task for component %q:\n%w", component.GetName(), err)
173173
}
174174

175175
return &binaryPackageTask{
@@ -186,7 +186,7 @@ func (t *binaryPackageTask) Run(ctx context.Context) ([]artifacts.Artifact, erro
186186

187187
rpmPaths, err := t.componentBuilder.BuildBinaryPackage(ctx, t.component, t.sourcePackagePath, outputDir, t.runChecks)
188188
if err != nil {
189-
return nil, fmt.Errorf("failed to build RPM for %#q: %w", t.component.GetName(), err)
189+
return nil, fmt.Errorf("failed to build RPM for %#q:\n%w", t.component.GetName(), err)
190190
}
191191

192192
if len(rpmPaths) == 0 {
@@ -215,7 +215,7 @@ func NewBinaryPackageTaskBuilder(
215215
) (*binaryPackageTaskBuilder, error) {
216216
builtTask, err := newBinaryPackageTask(componentBuilder, component, sourcePackagePath, runChecks)
217217
if err != nil {
218-
return nil, fmt.Errorf("failed to create binary package task: %w", err)
218+
return nil, fmt.Errorf("failed to create binary package task:\n%w", err)
219219
}
220220

221221
return &binaryPackageTaskBuilder{

internal/app/azldev/cmds/component/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func ListComponentConfigs(
5555

5656
comps, err = resolver.FindComponents(&options.ComponentFilter)
5757
if err != nil {
58-
return results, fmt.Errorf("failed to resolve components: %w", err)
58+
return results, fmt.Errorf("failed to resolve components:\n%w", err)
5959
}
6060

6161
// Extract the component configs from the resolved components, and return them in a slice.

0 commit comments

Comments
 (0)