Merge remote-tracking branch 'goog/mirror-aosp-master' into rvc-dev am: 300b6297f4

Change-Id: Iff1292b0504285196328fe2bd3c21dca03bfc35c
diff --git a/bootstrap/command.go b/bootstrap/command.go
index 1e3b2fe..cbbd32d 100644
--- a/bootstrap/command.go
+++ b/bootstrap/command.go
@@ -183,6 +183,12 @@
 	var f *os.File
 	var buf *bufio.Writer
 
+	if emptyNinjaFile {
+		if err := ioutil.WriteFile(absolutePath(outFile), []byte(nil), outFilePermissions); err != nil {
+			fatalf("error writing empty Ninja file: %s", err)
+		}
+	}
+
 	if stage != StageMain || !emptyNinjaFile {
 		f, err = os.OpenFile(absolutePath(outFile), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, outFilePermissions)
 		if err != nil {
diff --git a/context.go b/context.go
index da86837..9a3253a 100644
--- a/context.go
+++ b/context.go
@@ -218,7 +218,16 @@
 }
 
 func (module *moduleInfo) Name() string {
-	return module.group.name
+	// If this is called from a LoadHook (which is run before the module has been registered)
+	// then group will not be set and so the name is retrieved from logicModule.Name().
+	// Usually, using that method is not safe as it does not track renames (group.name does).
+	// However, when called from LoadHook it is safe as there is no way to rename a module
+	// until after the LoadHook has run and the module has been registered.
+	if module.group != nil {
+		return module.group.name
+	} else {
+		return module.logicModule.Name()
+	}
 }
 
 func (module *moduleInfo) String() string {
@@ -683,14 +692,18 @@
 		var scopedModuleFactories map[string]ModuleFactory
 
 		var addModule func(module *moduleInfo) []error
-		addModule = func(module *moduleInfo) (errs []error) {
-			moduleCh <- newModuleInfo{module, addedCh}
-			<-addedCh
-			var newModules []*moduleInfo
-			newModules, errs = runAndRemoveLoadHooks(c, config, module, &scopedModuleFactories)
+		addModule = func(module *moduleInfo) ([]error) {
+			// Run any load hooks immediately before it is sent to the moduleCh and is
+			// registered by name. This allows load hooks to set and/or modify any aspect
+			// of the module (including names) using information that is not available when
+			// the module factory is called.
+			newModules, errs := runAndRemoveLoadHooks(c, config, module, &scopedModuleFactories)
 			if len(errs) > 0 {
 				return errs
 			}
+
+			moduleCh <- newModuleInfo{module, addedCh}
+			<-addedCh
 			for _, n := range newModules {
 				errs = addModule(n)
 				if len(errs) > 0 {