@@ -549,6 +549,29 @@ export default class GithubPackage {
549549 * projects and pane items.
550550 */
551551 async getNextContext ( usePath = null ) {
552+ // Internal utility function to normalize paths not contained within a git
553+ // working tree.
554+ const workdirForNonGitPath = async sourcePath => {
555+ const containingRoot = this . project . getDirectories ( ) . find ( root => root . contains ( sourcePath ) ) ;
556+ if ( containingRoot ) {
557+ return containingRoot . getPath ( ) ;
558+ /* istanbul ignore else */
559+ } else if ( ! ( await fs . stat ( sourcePath ) ) . isDirectory ( ) ) {
560+ return path . dirname ( sourcePath ) ;
561+ } else {
562+ return sourcePath ;
563+ }
564+ } ;
565+
566+ // Internal utility function to identify the working directory to use for
567+ // an arbitrary (file or directory) path.
568+ const workdirForPath = async sourcePath => {
569+ return ( await Promise . all ( [
570+ this . workdirCache . find ( sourcePath ) ,
571+ workdirForNonGitPath ( sourcePath ) ,
572+ ] ) ) . find ( Boolean ) ;
573+ } ;
574+
552575 // Identify paths that *could* contribute a git working directory to the pool. This is drawn from
553576 // the roots of open projects, the currently locked context if one is present, and the path of the
554577 // open workspace item.
@@ -574,14 +597,16 @@ export default class GithubPackage {
574597 const workdirs = new Set (
575598 await Promise . all (
576599 Array . from ( candidatePaths , async candidatePath => {
577- const workdir = ( await this . workdirCache . find ( candidatePath ) ) || candidatePath ;
600+ const workdir = await workdirForPath ( candidatePath ) ;
601+
578602 // Note the workdirs associated with the active pane item and the first open project so we can
579603 // prefer them later.
580604 if ( candidatePath === activeItemPath ) {
581605 activeItemWorkdir = workdir ;
582606 } else if ( candidatePath === this . project . getPaths ( ) [ 0 ] ) {
583607 firstProjectWorkdir = workdir ;
584608 }
609+
585610 return workdir ;
586611 } ) ,
587612 ) ,
@@ -593,7 +618,17 @@ export default class GithubPackage {
593618 // 1 - Explicitly requested workdir. This is either selected by the user from a context tile or
594619 // deserialized from package state. Choose this context only if it still exists in the pool.
595620 if ( usePath ) {
596- const stateContext = this . contextPool . getContext ( usePath ) ;
621+ // Normalize usePath in a similar fashion to the way we do activeItemPath.
622+ let useWorkdir = usePath ;
623+ if ( usePath === activeItemPath ) {
624+ useWorkdir = activeItemWorkdir ;
625+ } else if ( usePath === this . project . getPaths ( ) [ 0 ] ) {
626+ useWorkdir = firstProjectWorkdir ;
627+ } else {
628+ useWorkdir = await workdirForPath ( usePath ) ;
629+ }
630+
631+ const stateContext = this . contextPool . getContext ( useWorkdir ) ;
597632 if ( stateContext . isPresent ( ) ) {
598633 return stateContext ;
599634 }
0 commit comments