@@ -1605,11 +1605,22 @@ def _normalize_dir(self, value):
1605
1605
value = os .path .abspath (value )
1606
1606
return value
1607
1607
1608
+ # Because the validation of preferred_dir depends on root_dir and validation
1609
+ # occurs when the trait is loaded, there are times when we should defer the
1610
+ # validation of preferred_dir (e.g., when preferred_dir is defined via CLI
1611
+ # and root_dir is defined via a config file).
1612
+ _defer_preferred_dir_validation = False
1613
+
1608
1614
@validate ("root_dir" )
1609
1615
def _root_dir_validate (self , proposal ):
1610
1616
value = self ._normalize_dir (proposal ["value" ])
1611
1617
if not os .path .isdir (value ):
1612
1618
raise TraitError (trans .gettext ("No such directory: '%r'" ) % value )
1619
+
1620
+ if self ._defer_preferred_dir_validation :
1621
+ # If we're here, then preferred_dir is configured on the CLI and
1622
+ # root_dir is configured in a file
1623
+ self ._preferred_dir_validation (self .preferred_dir , value )
1613
1624
return value
1614
1625
1615
1626
preferred_dir = Unicode (
@@ -1627,16 +1638,28 @@ def _preferred_dir_validate(self, proposal):
1627
1638
if not os .path .isdir (value ):
1628
1639
raise TraitError (trans .gettext ("No such preferred dir: '%r'" ) % value )
1629
1640
1630
- # preferred_dir must be equal or a subdir of root_dir
1631
- if not value .startswith (self .root_dir ):
1641
+ # Before we validate against root_dir, check if this trait is defined on the CLI
1642
+ # and root_dir is not. If that's the case, we'll defer it's further validation
1643
+ # until root_dir is validated or the server is starting (the latter occurs when
1644
+ # the default root_dir (cwd) is used).
1645
+ cli_config = self .cli_config .get ("ServerApp" , {})
1646
+ if "preferred_dir" in cli_config and "root_dir" not in cli_config :
1647
+ self ._defer_preferred_dir_validation = True
1648
+
1649
+ if not self ._defer_preferred_dir_validation : # Validate now
1650
+ self ._preferred_dir_validation (value , self .root_dir )
1651
+ return value
1652
+
1653
+ def _preferred_dir_validation (self , preferred_dir : str , root_dir : str ) -> None :
1654
+ """Validate preferred dir relative to root_dir - preferred_dir must be equal or a subdir of root_dir"""
1655
+ if not preferred_dir .startswith (root_dir ):
1632
1656
raise TraitError (
1633
1657
trans .gettext (
1634
1658
"preferred_dir must be equal or a subdir of root_dir. preferred_dir: '%r' root_dir: '%r'"
1635
1659
)
1636
- % (value , self . root_dir )
1660
+ % (preferred_dir , root_dir )
1637
1661
)
1638
-
1639
- return value
1662
+ self ._defer_preferred_dir_validation = False
1640
1663
1641
1664
@observe ("root_dir" )
1642
1665
def _root_dir_changed (self , change ):
@@ -2377,6 +2400,10 @@ def initialize(
2377
2400
# Parse command line, load ServerApp config files,
2378
2401
# and update ServerApp config.
2379
2402
super ().initialize (argv = argv )
2403
+ if self ._defer_preferred_dir_validation :
2404
+ # If we're here, then preferred_dir is configured on the CLI and
2405
+ # root_dir has the default value (cwd)
2406
+ self ._preferred_dir_validation (self .preferred_dir , self .root_dir )
2380
2407
if self ._dispatching :
2381
2408
return
2382
2409
# Then, use extensions' config loading mechanism to
0 commit comments