Skip to content

Commit

Permalink
Pass down context in all places where it's needed
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Apr 30, 2024
1 parent 693ca9a commit 4720df5
Show file tree
Hide file tree
Showing 51 changed files with 161 additions and 182 deletions.
8 changes: 3 additions & 5 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
}
} else {
// Load platforms from profile
errs := pmb.LoadHardwareForProfile(profile, true, downloadCallback, taskCallback, s.settings)
errs := pmb.LoadHardwareForProfile(ctx, profile, true, downloadCallback, taskCallback, s.settings)
for _, err := range errs {
s := &cmderrors.PlatformLoadingError{Cause: err}
responseError(s.GRPCStatus())
Expand Down Expand Up @@ -497,13 +497,11 @@ func (s *arduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesInd
}

// Perform index update
// TODO: pass context
// ctx := stream.Context()
config, err := s.settings.DownloaderConfig()
if err != nil {
return err
}
if err := globals.LibrariesIndexResource.Download(indexDir, downloadCB, config); err != nil {
if err := globals.LibrariesIndexResource.Download(stream.Context(), indexDir, downloadCB, config); err != nil {
resultCB(rpc.IndexUpdateReport_STATUS_FAILED)
return err
}
Expand Down Expand Up @@ -621,7 +619,7 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream
indexResource.SignatureURL, _ = url.Parse(u) // should not fail because we already parsed it
indexResource.SignatureURL.Path += ".sig"
}
if err := indexResource.Download(indexpath, downloadCB, config); err != nil {
if err := indexResource.Download(stream.Context(), indexpath, downloadCB, config); err != nil {
failed = true
result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(URL, rpc.IndexUpdateReport_STATUS_FAILED))
} else {
Expand Down
4 changes: 4 additions & 0 deletions internal/arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var ErrSketchCannotBeLocatedInBuildPath = errors.New("sketch cannot be located i

// Builder is a Sketch builder.
type Builder struct {
ctx context.Context

sketch *sketch.Sketch
buildProperties *properties.Map

Expand Down Expand Up @@ -198,6 +200,7 @@ func NewBuilder(

diagnosticStore := diagnostics.NewStore()
b := &Builder{
ctx: ctx,
sketch: sk,
buildProperties: buildProperties,
buildPath: buildPath,
Expand Down Expand Up @@ -305,6 +308,7 @@ func (b *Builder) preprocess() error {

b.logIfVerbose(false, tr("Detecting libraries used..."))
err := b.libsDetector.FindIncludes(
b.ctx,
b.buildPath,
b.buildProperties.GetPath("build.core.path"),
b.buildProperties.GetPath("build.variant.path"),
Expand Down
12 changes: 8 additions & 4 deletions internal/arduino/builder/internal/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package detector

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -196,6 +197,7 @@ func (l *SketchLibrariesDetector) appendIncludeFolder(

// FindIncludes todo
func (l *SketchLibrariesDetector) FindIncludes(
ctx context.Context,
buildPath *paths.Path,
buildCorePath *paths.Path,
buildVariantPath *paths.Path,
Expand All @@ -205,7 +207,7 @@ func (l *SketchLibrariesDetector) FindIncludes(
buildProperties *properties.Map,
platformArch string,
) error {
err := l.findIncludes(buildPath, buildCorePath, buildVariantPath, sketchBuildPath, sketch, librariesBuildPath, buildProperties, platformArch)
err := l.findIncludes(ctx, buildPath, buildCorePath, buildVariantPath, sketchBuildPath, sketch, librariesBuildPath, buildProperties, platformArch)
if err != nil && l.onlyUpdateCompilationDatabase {
l.logger.Info(
fmt.Sprintf(
Expand All @@ -220,6 +222,7 @@ func (l *SketchLibrariesDetector) FindIncludes(
}

func (l *SketchLibrariesDetector) findIncludes(
ctx context.Context,
buildPath *paths.Path,
buildCorePath *paths.Path,
buildVariantPath *paths.Path,
Expand Down Expand Up @@ -269,7 +272,7 @@ func (l *SketchLibrariesDetector) findIncludes(
}

for !sourceFileQueue.empty() {
err := l.findIncludesUntilDone(cache, sourceFileQueue, buildProperties, sketchBuildPath, librariesBuildPath, platformArch)
err := l.findIncludesUntilDone(ctx, cache, sourceFileQueue, buildProperties, sketchBuildPath, librariesBuildPath, platformArch)
if err != nil {
cachePath.Remove()
return err
Expand Down Expand Up @@ -297,6 +300,7 @@ func (l *SketchLibrariesDetector) findIncludes(
}

func (l *SketchLibrariesDetector) findIncludesUntilDone(
ctx context.Context,
cache *includeCache,
sourceFileQueue *uniqueSourceFileQueue,
buildProperties *properties.Map,
Expand Down Expand Up @@ -350,7 +354,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
l.logger.Info(tr("Using cached library dependencies for file: %[1]s", sourcePath))
}
} else {
preprocFirstResult, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties)
preprocFirstResult, preprocErr = preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
if l.logger.Verbose() {
l.logger.WriteStdout(preprocFirstResult.Stdout())
}
Expand Down Expand Up @@ -381,7 +385,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
// Library could not be resolved, show error
if preprocErr == nil || preprocFirstResult.Stderr() == nil {
// Filename came from cache, so run preprocessor to obtain error to show
result, err := preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties)
result, err := preprocessor.GCC(ctx, sourcePath, targetFilePath, includeFolders, buildProperties)
if l.logger.Verbose() {
l.logger.WriteStdout(result.Stdout())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
// PreprocessSketchWithArduinoPreprocessor performs preprocessing of the arduino sketch
// using arduino-preprocessor (https://github.com/arduino/arduino-preprocessor).
func PreprocessSketchWithArduinoPreprocessor(
ctx context.Context,
sk *sketch.Sketch, buildPath *paths.Path, includeFolders paths.PathList,
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
) (*Result, error) {
Expand All @@ -42,7 +43,7 @@ func PreprocessSketchWithArduinoPreprocessor(

sourceFile := buildPath.Join("sketch", sk.MainFile.Base()+".cpp")
targetFile := buildPath.Join("preproc", "sketch_merged.cpp")
gccResult, err := GCC(sourceFile, targetFile, includeFolders, buildProperties)
gccResult, err := GCC(ctx, sourceFile, targetFile, includeFolders, buildProperties)
verboseOut.Write(gccResult.Stdout())
verboseOut.Write(gccResult.Stderr())
if err != nil {
Expand Down Expand Up @@ -78,7 +79,7 @@ func PreprocessSketchWithArduinoPreprocessor(
}

verboseOut.WriteString(commandLine)
commandStdOut, commandStdErr, err := command.RunAndCaptureOutput(context.Background())
commandStdOut, commandStdErr, err := command.RunAndCaptureOutput(ctx)
verboseOut.Write(commandStdErr)
if err != nil {
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
Expand Down
9 changes: 5 additions & 4 deletions internal/arduino/builder/internal/preprocessor/ctags.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var DebugPreprocessor bool

// PreprocessSketchWithCtags performs preprocessing of the arduino sketch using CTags.
func PreprocessSketchWithCtags(
ctx context.Context,
sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList,
lineOffset int, buildProperties *properties.Map,
onlyUpdateCompilationDatabase, verbose bool,
Expand All @@ -57,7 +58,7 @@ func PreprocessSketchWithCtags(

// Run GCC preprocessor
sourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp")
result, err := GCC(sourceFile, ctagsTarget, includes, buildProperties)
result, err := GCC(ctx, sourceFile, ctagsTarget, includes, buildProperties)
stdout.Write(result.Stdout())
stderr.Write(result.Stderr())
if err != nil {
Expand All @@ -84,7 +85,7 @@ func PreprocessSketchWithCtags(
}

// Run CTags on gcc-preprocessed source
ctagsOutput, ctagsStdErr, err := RunCTags(ctagsTarget, buildProperties)
ctagsOutput, ctagsStdErr, err := RunCTags(ctx, ctagsTarget, buildProperties)
if verbose {
stderr.Write(ctagsStdErr)
}
Expand Down Expand Up @@ -179,7 +180,7 @@ func isFirstFunctionOutsideOfSource(firstFunctionLine int, sourceRows []string)
}

// RunCTags performs a run of ctags on the given source file. Returns the ctags output and the stderr contents.
func RunCTags(sourceFile *paths.Path, buildProperties *properties.Map) ([]byte, []byte, error) {
func RunCTags(ctx context.Context, sourceFile *paths.Path, buildProperties *properties.Map) ([]byte, []byte, error) {
ctagsBuildProperties := properties.NewMap()
ctagsBuildProperties.Set("tools.ctags.path", "{runtime.tools.ctags.path}")
ctagsBuildProperties.Set("tools.ctags.cmd.path", "{path}/ctags")
Expand All @@ -202,7 +203,7 @@ func RunCTags(sourceFile *paths.Path, buildProperties *properties.Map) ([]byte,
if err != nil {
return nil, nil, err
}
stdout, stderr, err := proc.RunAndCaptureOutput(context.Background())
stdout, stderr, err := proc.RunAndCaptureOutput(ctx)

// Append ctags arguments to stderr
args := fmt.Sprintln(strings.Join(parts, " "))
Expand Down
3 changes: 2 additions & 1 deletion internal/arduino/builder/internal/preprocessor/gcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
// GCC performs a run of the gcc preprocess (macro/includes expansion). The function outputs the result
// to targetFilePath. Returns the stdout/stderr of gcc if any.
func GCC(
ctx context.Context,
sourceFilePath, targetFilePath *paths.Path,
includes paths.PathList, buildProperties *properties.Map,
) (Result, error) {
Expand Down Expand Up @@ -75,7 +76,7 @@ func GCC(
if err != nil {
return Result{}, err
}
stdout, stderr, err := proc.RunAndCaptureOutput(context.Background())
stdout, stderr, err := proc.RunAndCaptureOutput(ctx)

// Append gcc arguments to stdout
stdout = append([]byte(fmt.Sprintln(strings.Join(args, " "))), stdout...)
Expand Down
1 change: 1 addition & 0 deletions internal/arduino/builder/preprocess_sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
func (b *Builder) preprocessSketch(includes paths.PathList) error {
// In the future we might change the preprocessor
result, err := preprocessor.PreprocessSketchWithCtags(
b.ctx,
b.sketch, b.buildPath, includes, b.lineOffset,
b.buildProperties, b.onlyUpdateCompilationDatabase, b.logger.Verbose(),
)
Expand Down
13 changes: 7 additions & 6 deletions internal/arduino/cores/packagemanager/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package packagemanager

import (
"context"
"fmt"
"net/url"

Expand All @@ -32,15 +33,15 @@ import (

// LoadHardwareForProfile load the hardware platforms for the given profile.
// If installMissing is true then possibly missing tools and platforms will be downloaded and installed.
func (pmb *Builder) LoadHardwareForProfile(p *sketch.Profile, installMissing bool, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB, settings *configuration.Settings) []error {
func (pmb *Builder) LoadHardwareForProfile(ctx context.Context, p *sketch.Profile, installMissing bool, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB, settings *configuration.Settings) []error {
pmb.profile = p

// Load required platforms
var merr []error
var platformReleases []*cores.PlatformRelease
indexURLs := map[string]*url.URL{}
for _, platformRef := range p.Platforms {
if platformRelease, err := pmb.loadProfilePlatform(platformRef, installMissing, downloadCB, taskCB, settings); err != nil {
if platformRelease, err := pmb.loadProfilePlatform(ctx, platformRef, installMissing, downloadCB, taskCB, settings); err != nil {
merr = append(merr, fmt.Errorf("%s: %w", tr("loading required platform %s", platformRef), err))
logrus.WithField("platform", platformRef).WithError(err).Debugf("Error loading platform for profile")
} else {
Expand Down Expand Up @@ -68,7 +69,7 @@ func (pmb *Builder) LoadHardwareForProfile(p *sketch.Profile, installMissing boo
return merr
}

func (pmb *Builder) loadProfilePlatform(platformRef *sketch.ProfilePlatformReference, installMissing bool, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB, settings *configuration.Settings) (*cores.PlatformRelease, error) {
func (pmb *Builder) loadProfilePlatform(ctx context.Context, platformRef *sketch.ProfilePlatformReference, installMissing bool, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB, settings *configuration.Settings) (*cores.PlatformRelease, error) {
targetPackage := pmb.packages.GetOrCreatePackage(platformRef.Packager)
platform := targetPackage.GetOrCreatePlatform(platformRef.Architecture)
release := platform.GetOrCreateRelease(platformRef.Version)
Expand All @@ -77,14 +78,14 @@ func (pmb *Builder) loadProfilePlatform(platformRef *sketch.ProfilePlatformRefer
destDir := settings.ProfilesCacheDir().Join(uid)
if !destDir.IsDir() && installMissing {
// Try installing the missing platform
if err := pmb.installMissingProfilePlatform(platformRef, destDir, downloadCB, taskCB); err != nil {
if err := pmb.installMissingProfilePlatform(ctx, platformRef, destDir, downloadCB, taskCB); err != nil {
return nil, err
}
}
return release, pmb.loadPlatformRelease(release, destDir)
}

func (pmb *Builder) installMissingProfilePlatform(platformRef *sketch.ProfilePlatformReference, destDir *paths.Path, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
func (pmb *Builder) installMissingProfilePlatform(ctx context.Context, platformRef *sketch.ProfilePlatformReference, destDir *paths.Path, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
// Instantiate a temporary package manager only for platform installation
_ = pmb.tempDir.MkdirAll()
tmp, err := paths.MkTempDir(pmb.tempDir.String(), "")
Expand All @@ -103,7 +104,7 @@ func (pmb *Builder) installMissingProfilePlatform(platformRef *sketch.ProfilePla
}
for _, indexURL := range indexesToDownload {
indexResource := resources.IndexResource{URL: indexURL}
if err := indexResource.Download(tmpPmb.IndexDir, downloadCB, pmb.downloaderConfig); err != nil {
if err := indexResource.Download(ctx, tmpPmb.IndexDir, downloadCB, pmb.downloaderConfig); err != nil {
taskCB(&rpc.TaskProgress{Name: tr("Error downloading %s", indexURL)})
return &cmderrors.FailedDownloadError{Message: tr("Error downloading %s", indexURL), Cause: err}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/arduino/resources/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (res *IndexResource) IndexFileName() (string, error) {

// Download will download the index and possibly check the signature using the Arduino's public key.
// If the file is in .gz format it will be unpacked first.
func (res *IndexResource) Download(destDir *paths.Path, downloadCB rpc.DownloadProgressCB, config downloader.Config) error {
func (res *IndexResource) Download(ctx context.Context, destDir *paths.Path, downloadCB rpc.DownloadProgressCB, config downloader.Config) error {
// Create destination directory
if err := destDir.MkdirAll(); err != nil {
return &cmderrors.PermissionDeniedError{Message: tr("Can't create data directory %s", destDir), Cause: err}
Expand Down Expand Up @@ -100,7 +100,7 @@ func (res *IndexResource) Download(destDir *paths.Path, downloadCB rpc.DownloadP
defer f.Close()
tmpArchivePath := tmp.Join("archive")
_ = tmpArchivePath.MkdirAll()
if err := extract.Bz2(context.Background(), f, tmpArchivePath.String(), nil); err != nil {
if err := extract.Bz2(ctx, f, tmpArchivePath.String(), nil); err != nil {
return &cmderrors.PermissionDeniedError{Message: tr("Error extracting %s", tmpIndexPath), Cause: err}
}

Expand Down
6 changes: 4 additions & 2 deletions internal/arduino/resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package resources

import (
"context"
"crypto"
"encoding/hex"
"fmt"
Expand Down Expand Up @@ -116,6 +117,7 @@ func TestDownloadAndChecksums(t *testing.T) {
}

func TestIndexDownloadAndSignatureWithinArchive(t *testing.T) {
ctx := context.Background()
// Spawn test webserver
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("testdata"))
Expand All @@ -132,7 +134,7 @@ func TestIndexDownloadAndSignatureWithinArchive(t *testing.T) {
destDir, err := paths.MkTempDir("", "")
require.NoError(t, err)
defer destDir.RemoveAll()
err = idxResource.Download(destDir, func(curr *rpc.DownloadProgress) {}, downloader.GetDefaultConfig())
err = idxResource.Download(ctx, destDir, func(curr *rpc.DownloadProgress) {}, downloader.GetDefaultConfig())
require.NoError(t, err)
require.True(t, destDir.Join("package_index.json").Exist())
require.True(t, destDir.Join("package_index.json.sig").Exist())
Expand All @@ -143,7 +145,7 @@ func TestIndexDownloadAndSignatureWithinArchive(t *testing.T) {
invDestDir, err := paths.MkTempDir("", "")
require.NoError(t, err)
defer invDestDir.RemoveAll()
err = invIdxResource.Download(invDestDir, func(curr *rpc.DownloadProgress) {}, downloader.GetDefaultConfig())
err = invIdxResource.Download(ctx, invDestDir, func(curr *rpc.DownloadProgress) {}, downloader.GetDefaultConfig())
require.Error(t, err)
require.Contains(t, err.Error(), "invalid signature")
require.False(t, invDestDir.Join("package_index.json").Exist())
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/arguments/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
func GetInstalledBoards(ctx context.Context, srv rpc.ArduinoCoreServiceServer) []string {
inst := instance.CreateAndInit(ctx, srv)

list, _ := srv.BoardListAll(context.Background(), &rpc.BoardListAllRequest{
list, _ := srv.BoardListAll(ctx, &rpc.BoardListAllRequest{
Instance: inst,
SearchArgs: nil,
IncludeHiddenBoards: false,
Expand All @@ -53,7 +53,7 @@ func GetInstalledProgrammers(ctx context.Context, srv rpc.ArduinoCoreServiceServ
SearchArgs: nil,
IncludeHiddenBoards: false,
}
list, _ := srv.BoardListAll(context.Background(), listAllReq)
list, _ := srv.BoardListAll(ctx, listAllReq)

installedProgrammers := make(map[string]string)
for _, board := range list.GetBoards() {
Expand Down Expand Up @@ -149,7 +149,7 @@ func getLibraries(ctx context.Context, srv rpc.ArduinoCoreServiceServer, all boo
func GetInstallableLibs(ctx context.Context, srv rpc.ArduinoCoreServiceServer) []string {
inst := instance.CreateAndInit(ctx, srv)

libs, _ := srv.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
libs, _ := srv.LibrarySearch(ctx, &rpc.LibrarySearchRequest{
Instance: inst,
SearchArgs: "", // if no query is specified all the libs are returned
})
Expand Down
8 changes: 4 additions & 4 deletions internal/cli/arguments/fqbn.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Fqbn struct {
func (f *Fqbn) AddToCommand(cmd *cobra.Command, srv rpc.ArduinoCoreServiceServer) {
cmd.Flags().StringVarP(&f.fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
cmd.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetInstalledBoards(context.Background(), srv), cobra.ShellCompDirectiveDefault
return GetInstalledBoards(cmd.Context(), srv), cobra.ShellCompDirectiveDefault
})
cmd.Flags().StringSliceVar(&f.boardOptions, "board-options", []string{},
tr("List of board options separated by commas. Or can be used multiple times for multiple options."))
Expand Down Expand Up @@ -70,7 +70,7 @@ func (f *Fqbn) Set(fqbn string) {
// - the port is not found, in this case nil is returned
// - the FQBN autodetection fail, in this case the function prints an error and
// terminates the execution
func CalculateFQBNAndPort(portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance, srv rpc.ArduinoCoreServiceServer, defaultFQBN, defaultAddress, defaultProtocol string) (string, *rpc.Port) {
func CalculateFQBNAndPort(ctx context.Context, portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance, srv rpc.ArduinoCoreServiceServer, defaultFQBN, defaultAddress, defaultProtocol string) (string, *rpc.Port) {
fqbn := fqbnArg.String()
if fqbn == "" {
fqbn = defaultFQBN
Expand All @@ -79,14 +79,14 @@ func CalculateFQBNAndPort(portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance,
if portArgs == nil || portArgs.address == "" {
feedback.FatalError(&cmderrors.MissingFQBNError{}, feedback.ErrGeneric)
}
fqbn, port := portArgs.DetectFQBN(instance, srv)
fqbn, port := portArgs.DetectFQBN(ctx, instance, srv)
if fqbn == "" {
feedback.FatalError(&cmderrors.MissingFQBNError{}, feedback.ErrGeneric)
}
return fqbn, port
}

port, err := portArgs.GetPort(instance, srv, defaultAddress, defaultProtocol)
port, err := portArgs.GetPort(ctx, instance, srv, defaultAddress, defaultProtocol)
if err != nil {
feedback.Fatal(tr("Error getting port metadata: %v", err), feedback.ErrGeneric)
}
Expand Down
Loading

0 comments on commit 4720df5

Please sign in to comment.