Skip to content

Commit a750bdc

Browse files
committed
Perform a deep-copy of upload ports where needed.
Previously only the pointer was copied, thus making changes in `actualPort` to be reflected also to `port`. This lead to some weird result in the `updatedUploadPort` result: { "stdout": "Verify 11344 bytes of flash with checksum.\nVerify successful\ndone in 0.010 seconds\nCPU reset.\n", "stderr": "", "updated_upload_port": { "address": "/dev/tty.usbmodem14101", <------- this address... "label": "/dev/cu.usbmodem14101", <------- ...is different from the label "protocol": "serial", "protocol_label": "Serial Port (USB)", "properties": { "pid": "0x804E", "serialNumber": "94A3397C5150435437202020FF150838", "vid": "0x2341" }, "hardware_id": "94A3397C5150435437202020FF150838" } }
1 parent 990d93a commit a750bdc

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

Diff for: arduino/discovery/discovery.go

+25
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,38 @@ func (p *Port) ToRPC() *rpc.Port {
113113
}
114114
}
115115

116+
// PortFromRPCPort converts an *rpc.Port to a *Port
117+
func PortFromRPCPort(o *rpc.Port) (p *Port) {
118+
if o == nil {
119+
return nil
120+
}
121+
return &Port{
122+
Address: o.Address,
123+
AddressLabel: o.Label,
124+
Protocol: o.Protocol,
125+
ProtocolLabel: o.ProtocolLabel,
126+
HardwareID: o.HardwareId,
127+
Properties: properties.NewFromHashmap(o.Properties),
128+
}
129+
}
130+
116131
func (p *Port) String() string {
117132
if p == nil {
118133
return "none"
119134
}
120135
return p.Address
121136
}
122137

138+
// Clone creates a copy of this Port
139+
func (p *Port) Clone() *Port {
140+
if p == nil {
141+
return nil
142+
}
143+
var res Port = *p
144+
res.Properties = p.Properties.Clone()
145+
return &res
146+
}
147+
123148
// Event is a pluggable discovery event
124149
type Event struct {
125150
Type string

Diff for: commands/upload/upload.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/arduino/arduino-cli/arduino"
2727
"github.com/arduino/arduino-cli/arduino/cores"
2828
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
29+
"github.com/arduino/arduino-cli/arduino/discovery"
2930
"github.com/arduino/arduino-cli/arduino/globals"
3031
"github.com/arduino/arduino-cli/arduino/serialutils"
3132
"github.com/arduino/arduino-cli/arduino/sketch"
@@ -212,10 +213,10 @@ func runProgramAction(pme *packagemanager.Explorer,
212213
go f.DiscardCh(watch)
213214
}()
214215

215-
port := userPort
216+
port := discovery.PortFromRPCPort(userPort)
216217
if port == nil || (port.Address == "" && port.Protocol == "") {
217218
// For no-port uploads use "default" protocol
218-
port = &rpc.Port{Protocol: "default"}
219+
port = &discovery.Port{Protocol: "default"}
219220
}
220221
logrus.WithField("port", port).Tracef("Upload port")
221222

@@ -421,7 +422,7 @@ func runProgramAction(pme *packagemanager.Explorer,
421422

422423
// If not using programmer perform some action required
423424
// to set the board in bootloader mode
424-
actualPort := port
425+
actualPort := port.Clone()
425426
if programmer == nil && !burnBootloader && (port.Protocol == "serial" || forcedSerialPortWait) {
426427
// Perform reset via 1200bps touch if requested and wait for upload port also if requested.
427428
touch := uploadProperties.GetBoolean("upload.use_1200bps_touch")
@@ -491,10 +492,10 @@ func runProgramAction(pme *packagemanager.Explorer,
491492

492493
// Get Port properties gathered using pluggable discovery
493494
uploadProperties.Set("upload.port.address", port.Address)
494-
uploadProperties.Set("upload.port.label", port.Label)
495+
uploadProperties.Set("upload.port.label", port.AddressLabel)
495496
uploadProperties.Set("upload.port.protocol", port.Protocol)
496497
uploadProperties.Set("upload.port.protocolLabel", port.ProtocolLabel)
497-
for prop, value := range actualPort.Properties {
498+
for prop, value := range actualPort.Properties.AsMap() {
498499
uploadProperties.Set(fmt.Sprintf("upload.port.properties.%s", prop), value)
499500
}
500501

@@ -527,7 +528,7 @@ func runProgramAction(pme *packagemanager.Explorer,
527528
return userPort, nil
528529
}
529530

530-
func detectUploadPort(uploadCtx context.Context, uploadPort *rpc.Port, watch <-chan *rpc.BoardListWatchResponse, result f.Future[*rpc.Port]) {
531+
func detectUploadPort(uploadCtx context.Context, uploadPort *discovery.Port, watch <-chan *rpc.BoardListWatchResponse, result f.Future[*rpc.Port]) {
531532
log := logrus.WithField("task", "port_detection")
532533
log.Tracef("Detecting new board port after upload")
533534

@@ -553,7 +554,7 @@ func detectUploadPort(uploadCtx context.Context, uploadPort *rpc.Port, watch <-c
553554
}
554555

555556
// Pick the first port that is detected after the upload
556-
desiredHwID := uploadPort.HardwareId
557+
desiredHwID := uploadPort.HardwareID
557558
timeout := time.After(5 * time.Second)
558559
for {
559560
select {

0 commit comments

Comments
 (0)