[cipd]Fix a bug when jiri update snapshot fails on packages.

After this patch, when cipd.Expand fails to generate expanded cipd
paths when checkout a snapshot file, jiri will not attempted to use
instance-ids from snapshot files. Instead, it will fall back to use
versions from manifests and log the expand failures into jiri debug
logs.

Bug: VC-155
Test: Local on arm64
Change-Id: Ic969ba1a29de867fd59cf2057fe6eca105624621
diff --git a/cipd/cipd.go b/cipd/cipd.go
index f629b18..ffa1e55 100644
--- a/cipd/cipd.go
+++ b/cipd/cipd.go
@@ -638,7 +638,8 @@
 }
 
 // Expand method expands a cipdPath that contains templates such as ${platform}
-// into concrete full paths.
+// into concrete full paths. It might return an empty slice if platforms
+// do not match the requirements in cipdPath.
 func Expand(cipdPath string, platforms []Platform) ([]string, error) {
 	output := make([]string, 0)
 	//expanders := make([]Expander, 0)
diff --git a/project/manifest.go b/project/manifest.go
index afd3d9c..86c035a 100644
--- a/project/manifest.go
+++ b/project/manifest.go
@@ -942,7 +942,7 @@
 
 	for _, pkg := range pkgs {
 
-		cipdDecl, err := pkg.cipdDecl(jirix.UsingSnapshot)
+		cipdDecl, err := pkg.cipdDecl(jirix)
 		if err != nil {
 			return "", err
 		}
@@ -961,7 +961,7 @@
 	return ensureFilePath, nil
 }
 
-func (p *Package) cipdDecl(usingSnapshot bool) (string, error) {
+func (p *Package) cipdDecl(jirix *jiri.X) (string, error) {
 	var buf bytes.Buffer
 	// Write "@Subdir" line to cipd declaration
 	subdir, err := p.GetPath()
@@ -983,23 +983,30 @@
 	}
 	var cipdPath, version string
 	version = p.Version
-	if usingSnapshot {
+	cipdPath, err = cipd.Decl(p.Name, plats)
+	if err != nil {
+		return "", err
+	}
+	if jirix.UsingSnapshot && len(p.Instances) != 0 {
 		candPath, err := cipd.Expand(p.Name, []cipd.Platform{cipd.CipdPlatform})
 		if err != nil {
 			return "", err
 		}
-		// since err != nil, candPath cannot be empty
-		cipdPath = candPath[0]
-		for _, inst := range p.Instances {
-			if inst.Name == cipdPath {
-				version = inst.ID
-				break
+		if len(candPath) > 0 {
+			for _, inst := range p.Instances {
+				if inst.Name == candPath[0] {
+					cipdPath = candPath[0]
+					version = inst.ID
+					break
+				}
 			}
-		}
-	} else {
-		cipdPath, err = cipd.Decl(p.Name, plats)
-		if err != nil {
-			return "", err
+		} else {
+			// cipd.Expand failed expand
+			// This may happen if the cipdPath does not allow platform
+			// in cipd.CipdPlatform. E.g.
+			// "example/linux-${arch=amd64}" expanded with "linux-arm64".
+			// Leave a log in Debug log.
+			jirix.Logger.Debugf("cipd.Expand failed to expand cipd path %q using platforms %v", p.Name, cipd.CipdPlatform)
 		}
 	}
 	buf.WriteString(fmt.Sprintf("%s %s\n", cipdPath, version))